@@ -50,16 +50,39 @@ def classifier(self, videoDeviceId = 0):
5050 success , img = self .videoCapture .read ()
5151 if success :
5252 features = []
53+
54+ EI_CLASSIFIER_INPUT_WIDTH = self .dim [0 ]
55+ EI_CLASSIFIER_INPUT_HEIGHT = self .dim [1 ]
56+
57+ in_frame_cols = img .shape [1 ]
58+ in_frame_rows = img .shape [0 ]
59+
60+ factor_w = EI_CLASSIFIER_INPUT_WIDTH / in_frame_cols
61+ factor_h = EI_CLASSIFIER_INPUT_HEIGHT / in_frame_rows
62+
63+ largest_factor = factor_w if factor_w > factor_h else factor_h
64+
65+ resize_size_w = int (largest_factor * in_frame_cols )
66+ resize_size_h = int (largest_factor * in_frame_rows )
67+ resize_size = (resize_size_w , resize_size_h )
68+
69+ resized = cv2 .resize (img , resize_size , interpolation = cv2 .INTER_AREA )
70+
71+ crop_x = int ((resize_size_w - resize_size_h ) / 2 ) if resize_size_w > resize_size_h else 0
72+ crop_y = int ((resize_size_h - resize_size_w ) / 2 ) if resize_size_h > resize_size_w else 0
73+
74+ crop_region = (crop_x , crop_y , EI_CLASSIFIER_INPUT_WIDTH , EI_CLASSIFIER_INPUT_HEIGHT )
75+
76+ cropped = resized [crop_region [1 ]:crop_region [1 ]+ crop_region [3 ], crop_region [0 ]:crop_region [0 ]+ crop_region [2 ]]
77+
5378 if self .isGrayscale :
54- img = cv2 .cvtColor (img , cv2 .COLOR_BGR2GRAY )
55- resizedImg = cv2 .resize (img , self .dim , interpolation = cv2 .INTER_AREA )
56- pixels = np .array (resizedImg ).flatten ().tolist ()
79+ cropped = cv2 .cvtColor (cropped , cv2 .COLOR_BGR2GRAY )
80+ pixels = np .array (cropped ).flatten ().tolist ()
5781
5882 for p in pixels :
5983 features .append ((p << 16 ) + (p << 8 ) + p )
6084 else :
61- resizedImg = cv2 .resize (img , self .dim , interpolation = cv2 .INTER_AREA )
62- pixels = np .array (resizedImg ).flatten ().tolist ()
85+ pixels = np .array (cropped ).flatten ().tolist ()
6386
6487 for ix in range (0 , len (pixels ), 3 ):
6588 b = pixels [ix + 0 ]
@@ -68,4 +91,4 @@ def classifier(self, videoDeviceId = 0):
6891 features .append ((r << 16 ) + (g << 8 ) + b )
6992
7093 res = self .classify (features )
71- yield res , img
94+ yield res , cropped
0 commit comments