-
Notifications
You must be signed in to change notification settings - Fork 5
/
recognise_from_webcam.py
75 lines (57 loc) · 1.88 KB
/
recognise_from_webcam.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import time
import sys
import cv2
from threading import Thread
from RBM_core import *
###############################################################################
# Classification of images from computer's webcam
# Run with "-s" arg to run in silent mode: to not show plots
show_plots = True
for arg in sys.argv:
if arg == "-s" or arg == "-S":
show_plots = False
process_next_image = True
def greyscale_image_and_predict(frame):
global process_next_image
global show_plots
img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img = Image.fromarray(img)
local_img = convert_image_for_network(img)
predict_2D_image(
local_img,
classifier,
show_plot=show_plots
)
process_next_image = True
print("==============================================================================")
print(' Now predicting numbers for webcam pictures')
print("==============================================================================")
cv2.namedWindow('Normal view, press ESC to exit')
try:
vc = cv2.VideoCapture(0)
if vc.isOpened(): # try to get the first frame
rval, frame = vc.read()
else:
rval = False
while rval:
cv2.imshow("Normal view, press ESC to exit", frame)
time.sleep(0.15) # Delay
rval, frame = vc.read()
if (process_next_image):
process_next_image = False
threaded_func = Thread(
target=greyscale_image_and_predict,
args=([frame])
)
threaded_func.start()
key = cv2.waitKey(20)
if key == 27: # exit on ESC
cv2.destroyWindow("Normal view, press ESC to exit")
break
finally:
# Free camera resources
del vc
threaded_func.join()
print("")
print("______________________________________________________________________________")
print("Done.")