-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathcamera_test.py
More file actions
64 lines (50 loc) · 1.41 KB
/
camera_test.py
File metadata and controls
64 lines (50 loc) · 1.41 KB
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
from keras.preprocessing import image as image_utils
from imagenet_utils import decode_predictions
from imagenet_utils import preprocess_input
from vgg16 import VGG16
import argparse
import cv2
import numpy as np
import os
import random
import sys
import threading
label = ''
frame = None
class MyThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
global label
# Load the VGG16 network
print("[INFO] loading network...")
self.model = VGG16(weights="imagenet")
while (~(frame is None)):
(inID, label) = self.predict(frame)
def predict(self, frame):
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB).astype(np.float32)
image = image.transpose((2, 0, 1))
image = image.reshape((1,) + image.shape)
image = preprocess_input(image)
preds = self.model.predict(image)
return decode_predictions(preds)[0]
cap = cv2.VideoCapture(0)
if (cap.isOpened()):
print("Camera OK")
else:
cap.open()
keras_thread = MyThread()
keras_thread.start()
while (True):
ret, original = cap.read()
frame = cv2.resize(original, (224, 224))
# Display the predictions
# print("ImageNet ID: {}, Label: {}".format(inID, label))
cv2.putText(original, "Label: {}".format(label), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
cv2.imshow("Classification", original)
if (cv2.waitKey(1) & 0xFF == ord('q')):
break;
cap.release()
frame = None
cv2.destroyAllWindows()
sys.exit()