OpenCV3 write text on video

asked 2017-07-12 04:14:11 -0500

Derick gravatar image

I has raspberry pi 3 with raspbian jessie installed. Open CV3 and python 3.0 also installed. I get a python sample code which detect the face. I need write text on the screen but it's not write on it. I need write text once instead of repeated on top of each faces. Below is the code

import cv2
import sys
from tkinter import *
from tkinter import messagebox

faceCascade =  cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
video_capture = cv2.VideoCapture(0)
video_capture.set(3,500)
video_capture.set(4,300)
video_capture.set(12, 0.1)

frameWidth = int(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH))
frameHeight = int(video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT))

while True:
   ret, frame = video_capture.read()
   gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.CASCADE_SCALE_IMAGE

)

# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
#cv2.putText(frame, 'How are you', (x - 1, y - 1), cv2.FONT_HERSHEY_PLAIN,2,(0, 255, 0)) 
#if the puttext at here, it will write the text on top of each face detected.  But I just need the text appear once.

# Display the resulting frame
cv2.imshow('Video', frame)


if len(faces) > 0:
    cv2.putText(img = frame, text = 'How are you', org = (int(frameWidth/2 - 20),int(frameHeight/2)), fontFace = cv2.FONT_HERSHEY_DUPLEX, fontScale = 3, 
                color = (0, 255, 0))
#print(int(frameWidth/2 - 20),int(frameHeight/2))
#print('Found ' + str(len(faces)) + ' face(s)')

if cv2.waitKey(1) & 0xFF == ord('q'):
    break

# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
edit retag flag offensive close merge delete

Comments

why not use putText outside if statement in other if statement similar to these? (sorry in c++ langh)

    if((len(faces) > 0) && firstimeToWrite){
    cv::putText(img = frame, text = 'How are you', org = (int(frameWidth/2 - 20),int(frameHeight/2)), fontFace = cv::FONT_HERSHEY_DUPLEX, fontScale = 3, 
                color = (0, 255, 0));
 firstimeToWrite = false;
}
gfx gravatar imagegfx ( 2017-07-12 04:54:51 -0500 )edit