我希望在python中使用openCV关闭3秒后检测闭眼。但是当我用time.sleep(1)来计算时间时,整个程序就停止了。 但该计划必须持续运行以检测闭目的眼睛。 我认为可以在python中使用线程
def get_frame(self):
success, image = self.video.read()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.3,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.cv.CV_HAAR_SCALE_IMAGE
)
while True:
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x + w, y + h), (255, 255, 0), 2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = image[y:y+h, x:x+w]
eyes = eyesCascade.detectMultiScale(roi_gray)
if eyes is not():
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex -10 ,ey - 10),(ex+ew + 10,ey+eh + 10),(0,255,0),2)
twoeyes = twoeyesCascade.detectMultiScale(roi_gray)
checkyeys = 0
if twoeyes is not():
for (exx,eyy,eww,ehh) in twoeyes:
checkyeys = 0
led.write(1)
cv2.rectangle(roi_color,(exx-5 ,eyy -5 ),(exx+eww -5,eyy+ehh -5 ),(0,0, 255),2)
else:
#when eyes close
print "------------------------------------"
for i in xrange(10):
time.sleep(1)
if(i % 3 == 0){
#eyes close in 3 seconds
print "Warning"
}
print i
ret, jpeg = cv2.imencode('.jpg', image)
self.string = jpeg.tostring()
self._image = image
return jpeg.tostring()
感谢帮助!!!
答案 0 :(得分:0)
尝试更改
if(i % 3 == 0){
#eyes close in 3 seconds
print "Warning"
}
到
if i % 3 == 0:
#eyes close in 3 seconds
print "Warning"