-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopencv.py
More file actions
69 lines (54 loc) · 1.98 KB
/
opencv.py
File metadata and controls
69 lines (54 loc) · 1.98 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
65
66
67
68
69
import cv2
import numpy as np
from keras.models import load_model
from skimage.transform import resize, pyramid_reduce
# Load your pre-trained model
model = load_model('model.h5')
def get_square(image, square_size):
height, width = image.shape
if height > width:
differ = height
else:
differ = width
differ += 4
mask = np.zeros((differ, differ), dtype="uint8")
x_pos = int((differ - width) / 2)
y_pos = int((differ - height) / 2)
mask[y_pos: y_pos + height, x_pos: x_pos + width] = image[0: height, 0: width]
if differ / square_size > 1:
mask = pyramid_reduce(mask, differ / square_size)
else:
mask = cv2.resize(mask, (square_size, square_size), interpolation=cv2.INTER_AREA)
return mask
def keras_predict(model, image):
data = np.asarray(image, dtype="int32")
pred_probab = model.predict(data)[0]
pred_class = list(pred_probab).index(max(pred_probab))
return max(pred_probab), pred_class
def keras_process_image(img):
image_x = 28
image_y = 28
img = get_square(img, 28)
img = np.reshape(img, (image_x, image_y))
return img
def crop_image(image, x, y, width, height):
return image[y:y + height, x:x + width]
cam_capture = cv2.VideoCapture(0)
while True:
_, image_frame = cam_capture.read()
# Select ROI - replace these with actual bbox coordinates
im2 = crop_image(image_frame, 300, 300, 300, 300)
image_grayscale = cv2.cvtColor(im2, cv2.COLOR_BGR2GRAY)
image_grayscale_blurred = cv2.GaussianBlur(image_grayscale, (15,15), 0)
resized_img = keras_process_image(image_grayscale_blurred)
ar = resized_img.reshape(1, 784)
pred_probab, pred_class = keras_predict(model, ar)
print(pred_class, pred_probab)
# Display cropped image
cv2.imshow("Image2", im2)
cv2.imshow("Image4", resized_img)
cv2.imshow("Image3", image_grayscale_blurred)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
cam_capture.release()
cv2.destroyAllWindows()