Commit 46440f50 authored by tihmels's avatar tihmels

Video Input Processing in Thread ausgelagert

parent ec6fc745
from threading import Thread
import cv2
class WebcamVideoStream:
def __init__(self, src=0):
# initialize the video camera stream and read the first frame
# from the stream
self.stream = cv2.VideoCapture(src)
(self.grabbed, self.frame) = self.stream.read()
# initialize the variable used to indicate if the thread should
# be stopped
self.stopped = False
def start(self):
# start the thread to read frames from the video stream
Thread(target=self.update, args=()).start()
return self
def update(self):
# keep looping infinitely until the thread is stopped
while True:
# if the thread indicator variable is set, stop the thread
if self.stopped:
return
# otherwise, read the next frame from the stream
(self.grabbed, self.frame) = self.stream.read()
def read(self):
# return the frame most recently read
return self.frame
def stop(self):
# indicate that the thread should be stopped
self.stopped = True
\ No newline at end of file
......@@ -8,7 +8,7 @@ from cv2 import WINDOW_NORMAL
import cv2
from WebcamVideoStream import WebcamVideoStream
from face_detect import find_faces
from image_commons import nparray_as_image, draw_with_alpha
......@@ -36,26 +36,23 @@ def show_webcam_and_run(model, emoticons, window_size=(800, 800), window_name='M
width, height = window_size
cv2.resizeWindow(window_name, width, height)
vc = cv2.VideoCapture(0)
if vc.isOpened():
read_value, webcam_image = vc.read()
else:
print("Webcam nicht gefunden")
return
vc = WebcamVideoStream(src=0).start()
frame = vc.read()
while read_value:
for normalized_face, (x, y, w, h) in find_faces(webcam_image):
while True:
for normalized_face, (x, y, w, h) in find_faces(frame):
prediction = model.predict(normalized_face) # do prediction
image_to_draw = emoticons[(prediction[0])]
if x - 150 > 0 and y - 50 > 0 and w - 150 > 0 and h - 150 > 0:
draw_with_alpha(webcam_image, image_to_draw, (x-150, y-50, w-150, h-150))
draw_with_alpha(frame, image_to_draw, (x-150, y-50, w-150, h-150))
cv2.imshow(window_name, webcam_image)
read_value, webcam_image = vc.read()
cv2.imshow(window_name, frame)
frame = vc.read()
key = cv2.waitKey(update_time)
if key == 27: # exit on ESC
vc.stop()
break
cv2.destroyWindow(window_name)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment