Skip to content

Commit 050222c

Browse files
committed
Fix resizing / cropping image, and return cropped image from imageclassifier
1 parent b30f6fb commit 050222c

3 files changed

Lines changed: 40 additions & 14 deletions

File tree

edge_impulse_linux/image.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

examples/image/classify.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from edge_impulse_linux.image import ImageImpulseRunner
99

1010
runner = None
11+
show_camera = False
1112

1213
def now():
1314
return round(time.time() * 1000)
@@ -80,7 +81,7 @@ def main(argv):
8081
camera = cv2.VideoCapture(videoCaptureDeviceId)
8182
ret = camera.read()[0]
8283
if ret:
83-
backendName =camera.getBackendName()
84+
backendName = camera.getBackendName()
8485
w = camera.get(3)
8586
h = camera.get(4)
8687
print("Camera %s (%s x %s) in port %s selected." %(backendName,h,w, videoCaptureDeviceId))
@@ -94,18 +95,20 @@ def main(argv):
9495
if (next_frame > now()):
9596
time.sleep((next_frame - now()) / 1000)
9697

97-
#print(res["result"])
98+
# print('classification runner response', res)
99+
98100
if "classification" in res["result"].keys():
99101
print('Result (%d ms.) ' % (res['timing']['dsp'] + res['timing']['classification']), end='')
100102
for label in labels:
101103
score = res['result']['classification'][label]
102104
print('%s: %.2f\t' % (label, score), end='')
103105
print('', flush=True)
104-
#cv2.imshow('edgeimpulse',img)
105-
#if cv2.waitKey(1) == ord('q'):
106-
# break
107-
108-
106+
107+
if (show_camera):
108+
cv2.imshow('edgeimpulse', img)
109+
if cv2.waitKey(1) == ord('q'):
110+
break
111+
109112
elif "bounding_boxes" in res["result"].keys():
110113
print('Found %d bounding boxes (%d ms.)' % (len(res["result"]["bounding_boxes"]), res['timing']['dsp'] + res['timing']['classification']))
111114
for bb in res["result"]["bounding_boxes"]:

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = edge_impulse_linux
3-
version = 1.0.0
3+
version = 1.0.1
44
author = EdgeImpulse Inc.
55
author_email = hello@edgeimpulse.com
66
description = Python runner for real-time ML classification

0 commit comments

Comments
 (0)