1
1
import cv2
2
- from matplotlib import pyplot as plt
3
- import numpy as np
2
+ import numpy as np
4
3
from keras .models import load_model
4
+ from skimage .transform import resize , pyramid_reduce
5
5
6
6
model = load_model ('model.h5' )
7
7
8
+ def get_square (image , square_size ):
9
+
10
+ height , width = image .shape
11
+ if (height > width ):
12
+ differ = height
13
+ else :
14
+ differ = width
15
+ differ += 4
16
+
17
+
18
+ mask = np .zeros ((differ , differ ), dtype = "uint8" )
19
+
20
+ x_pos = int ((differ - width ) / 2 )
21
+ y_pos = int ((differ - height ) / 2 )
22
+
23
+
24
+ mask [y_pos : y_pos + height , x_pos : x_pos + width ] = image [0 : height , 0 : width ]
25
+
26
+
27
+ if differ / square_size > 1 :
28
+ mask = pyramid_reduce (mask , differ / square_size )
29
+ else :
30
+ mask = cv2 .resize (mask , (square_size , square_size ), interpolation = cv2 .INTER_AREA )
31
+ return mask
8
32
9
33
10
34
def keras_predict (model , image ):
@@ -14,66 +38,51 @@ def keras_predict(model, image):
14
38
pred_class = list (pred_probab ).index (max (pred_probab ))
15
39
return max (pred_probab ), pred_class
16
40
17
-
18
-
19
41
def keras_process_image (img ):
42
+
20
43
image_x = 28
21
44
image_y = 28
22
- img = cv2 .resize (img , (image_x , image_y ))
23
- img = np .array (img , dtype = np .float32 )
24
- img = np .reshape (img , (- 1 , image_x , image_y , 1 ))
25
- return img
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
- def sketch_transform (image ):
34
- image_grayscale = cv2 .cvtColor (image , cv2 .COLOR_BGR2GRAY )
35
- image_grayscale_blurred = cv2 .GaussianBlur (image_grayscale , (15 ,15 ), 0 )
45
+ #img = cv2.resize(img, (28,28), interpolation = cv2.INTER_AREA)
46
+ img = get_square (img , 28 )
47
+ img = np .reshape (img , (image_x , image_y ))
36
48
37
49
38
- return image_grayscale_blurred
39
-
40
-
41
-
42
-
43
- cam_capture = cv2 .VideoCapture (0 )
44
-
45
- upper_left = (300 , 300 )
46
- bottom_right = (1084 , 1084 )
47
-
50
+ return img
51
+
48
52
53
+ def crop_image (image , x , y , width , height ):
54
+ return image [y :y + height , x :x + width ]
49
55
50
- while True :
56
+ while True :
51
57
cam_capture = cv2 .VideoCapture (0 )
58
+ _ , image_frame = cam_capture .read ()
59
+ # Select ROI
60
+ im2 = crop_image (image_frame , 300 ,300 ,300 ,300 )
61
+ image_grayscale = cv2 .cvtColor (im2 , cv2 .COLOR_BGR2GRAY )
62
+ image_grayscale_blurred = cv2 .GaussianBlur (image_grayscale , (15 ,15 ), 0 )
52
63
53
- upper_left = (300 , 300 )
54
- bottom_right = (1084 , 1084 )
55
64
56
- _ , image_frame = cam_capture .read ()
57
-
58
- #Rectangle marker
59
- r = cv2 .rectangle (image_frame , upper_left , bottom_right , (100 , 50 , 200 ), 5 )
60
- rect_img = image_frame [upper_left [1 ] : bottom_right [1 ], upper_left [0 ] : bottom_right [0 ]]
61
-
62
- sketcher_rect = rect_img
63
- sketcher_rect = sketch_transform (sketcher_rect )
64
-
65
-
66
-
67
- cv2 .resize (sketcher_rect , (28 ,28 ), interpolation = cv2 .INTER_AREA )
68
- pred_probab , pred_class = keras_predict (model , sketcher_rect )
69
- print (pred_class , pred_probab )
70
- cv2 .imshow ('image_frame' ,sketcher_rect )
65
+ #resized_img = image_resize(image_grayscale_blurred, width = 28, height = 28, inter = cv2.INTER_AREA)
66
+ #resized_img = keras_process_image(image_grayscale_blurred)
67
+ resized_img = cv2 .resize (image_grayscale_blurred ,(28 ,28 ))
68
+ #ar = np.array(resized_img)
69
+ ar = resized_img .reshape (1 ,784 )
71
70
71
+ pred_probab , pred_class = keras_predict (model , ar )
72
+ print (pred_class , pred_probab )
73
+
72
74
75
+
76
+ # Display cropped image
77
+
78
+ cv2 .imshow ("Image2" ,im2 )
79
+ cv2 .imshow ("Image4" ,resized_img )
80
+ cv2 .imshow ("Image3" ,image_grayscale_blurred )
73
81
74
82
if cv2 .waitKey (25 ) & 0xFF == ord ('q' ):
75
83
cv2 .destroyAllWindows ()
76
84
break
77
-
85
+
86
+
78
87
cam_capture .release ()
79
- cv2 .destroyAllWindows ()
88
+ cv2 .destroyAllWindows ()
0 commit comments