Skip to content

Commit 07704b3

Browse files
Added conversion from BGR to RGB (#8)
* Added conversion from BGR to RGB cv2.imread() returns images in BGR format (https://docs.opencv.org/4.5.2/d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56). Added function to convert to RGB. * BRG to RGB conversion everywhere Co-authored-by: Jan Jongboom <janjongboom@gmail.com>
1 parent 2619ec5 commit 07704b3

5 files changed

Lines changed: 24 additions & 9 deletions

File tree

edge_impulse_linux/image.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ def __init__(self, model_path: str):
1616
self.isGrayscale = False
1717

1818
def init(self):
19-
if psutil.OSX or psutil.MACOS:
20-
print('Make sure that video devices access is granted for your application.')
21-
print('If your video device is not responding, try running "tccutil reset Camera" to reset the camera access privileges')
22-
2319
model_info = super(ImageImpulseRunner, self).init()
2420
width = model_info['model_parameters']['image_input_width'];
2521
height = model_info['model_parameters']['image_input_height'];
@@ -44,23 +40,36 @@ def __exit__(self, type, value, traceback):
4440
def classify(self, data):
4541
return super(ImageImpulseRunner, self).classify(data)
4642

43+
# This returns images in RGB format (not BGR)
4744
def get_frames(self, videoDeviceId = 0):
45+
if psutil.OSX or psutil.MACOS:
46+
print('Make sure to grant the this script access to your webcam.')
47+
print('If your webcam is not responding, try running "tccutil reset Camera" to reset the camera access privileges.')
48+
4849
self.videoCapture = cv2.VideoCapture(videoDeviceId)
4950
while not self.closed:
5051
success, img = self.videoCapture.read()
52+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
5153
if success:
5254
yield img
5355

56+
# This returns images in RGB format (not BGR)
5457
def classifier(self, videoDeviceId = 0):
58+
if psutil.OSX or psutil.MACOS:
59+
print('Make sure to grant the this script access to your webcam.')
60+
print('If your webcam is not responding, try running "tccutil reset Camera" to reset the camera access privileges.')
61+
5562
self.videoCapture = cv2.VideoCapture(videoDeviceId)
5663
while not self.closed:
5764
success, img = self.videoCapture.read()
5865
if success:
66+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
5967
features, cropped = self.get_features_from_image(img)
6068

6169
res = self.classify(features)
6270
yield res, cropped
6371

72+
# This expects images in RGB format (not BGR)
6473
def get_features_from_image(self, img, crop_direction_x='center', crop_direction_y='center'):
6574
features = []
6675

examples/image/classify-full-frame.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ def main(argv):
103103
res_l = runner.classify(features_l)
104104
res_r = runner.classify(features_r)
105105

106-
cv2.imwrite('debug_l.jpg', cropped_l)
107-
cv2.imwrite('debug_r.jpg', cropped_r)
106+
cv2.imwrite('debug_l.jpg', cv2.cvtColor(cropped_l, cv2.COLOR_RGB2BGR))
107+
cv2.imwrite('debug_r.jpg', cv2.cvtColor(cropped_r, cv2.COLOR_RGB2BGR))
108108

109109
def print_classification(res, tag):
110110
if "classification" in res["result"].keys():

examples/image/classify-image.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,19 @@ def main(argv):
4141
labels = model_info['model_parameters']['labels']
4242

4343
img = cv2.imread(args[1])
44+
if img is None:
45+
print('Failed to load image', args[1])
46+
exit(1)
47+
48+
# imread returns images in BGR format, so we need to convert to RGB
49+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
4450

4551
# get_features_from_image also takes a crop direction arguments in case you don't have square images
4652
features, cropped = runner.get_features_from_image(img)
4753

4854
# the image will be resized and cropped, save a copy of the picture here
4955
# so you can see what's being passed into the classifier
50-
cv2.imwrite('debug.jpg', cropped)
56+
cv2.imwrite('debug.jpg', cv2.cvtColor(cropped, cv2.COLOR_RGB2BGR))
5157

5258
res = runner.classify(features)
5359

examples/image/classify.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def main(argv):
111111
img = cv2.rectangle(img, (bb['x'], bb['y']), (bb['x'] + bb['width'], bb['y'] + bb['height']), (255, 0, 0), 1)
112112

113113
if (show_camera):
114-
cv2.imshow('edgeimpulse', img)
114+
cv2.imshow('edgeimpulse', cv2.cvtColor(img, cv2.COLOR_RGB2BGR))
115115
if cv2.waitKey(1) == ord('q'):
116116
break
117117

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.4
3+
version = 1.0.5
44
author = EdgeImpulse Inc.
55
author_email = hello@edgeimpulse.com
66
description = Python runner for real-time ML classification

0 commit comments

Comments
 (0)