Skip to content

Commit ae33125

Browse files
committed
Nitpicky fixes in examples
1 parent 94beeac commit ae33125

File tree

5 files changed

+19
-20
lines changed

5 files changed

+19
-20
lines changed

examples/ex01_hello_opencv.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# Standard OpenCV leverages the host operating system to access hardware, but we
1818
# don't have that luxury in MicroPython. Instead, drivers are provided for
1919
# various hardware components, which need to be initialized before using them.
20-
# The exmples import a module called `cv2_hardware_init`, which initializes the
20+
# The examples import a module called `cv2_hardware_init`, which initializes the
2121
# drivers. You may need to edit the contents of the `cv2_hardware_init` module
2222
# based on your specific board and hardware configuration
2323
from cv2_hardware_init import *
@@ -61,7 +61,7 @@
6161
cv.imshow(display, img) # Can alternatively call `display.imshow(img)`
6262

6363
# Standard OpenCV requires a call to `cv.waitKey()` to process events and
64-
# actually display the image. However the display driver shows the image
64+
# actually display the image. However, the display driver shows the image
6565
# immediately, so it's not necessary to call `cv.waitKey()` in MicroPython.
6666
# But it is available, and behaves almost like any other Python environment! The
6767
# only difference is that it requires a key to be pressed in the REPL instead of

examples/ex02_camera.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
success, frame = camera.read()
3535

3636
# Check if the frame was read successfully
37-
if success == False:
37+
if not success:
3838
print("Error reading frame from camera")
3939
break
4040

examples/ex03_touch_screen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
else:
5353
# Check if there was touch input before
5454
if touch_input:
55-
# There was touch input before, but not any more
55+
# There was touch input before, but not anymore
5656
touch_input = False
5757

5858
# Draw a line if there was touch input

examples/ex05_performance.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
# Loop to continuously read frames from the camera and display them
5757
while True:
5858
# Read a frame from the camera and measure how long it takes. Try running
59-
# this both with and without the preallocated `frame` array to see the
59+
# this both with and without the pre-allocated `frame` array to see the
6060
# difference in performance
6161
t0 = time.ticks_us()
6262
success, frame = camera.read(frame)
@@ -69,7 +69,7 @@
6969
break
7070

7171
# Now we'll do some processing on the frame. Try running this with and
72-
# without the preallocated `result_image` array, and try different OpenCV
72+
# without the pre-allocated `result_image` array, and try different OpenCV
7373
# functions to compare performance
7474
t0 = time.ticks_us()
7575
result_image = cv.cvtColor(frame, cv.COLOR_BGR2HSV, result_image)
@@ -110,21 +110,20 @@
110110
# since it mitigates how frequently garbage collection is triggered
111111
if memory_used < 0:
112112
print("Garbage collection triggered!")
113-
113+
114114
# Something to try is triggering the garbage collector manually each loop
115115
# iteration to immediately free up memory. Garbage collection can be faster
116116
# if less memory has been allocated, so this can help avoid long stutters
117-
# from occasional garbage collection. However garbage collection will always
118-
# take *some* time, so this will lower the average FPS. You can choose to do
119-
# this if you prefer a consistent frame rate, or don't if you prefer maximum
120-
# frame rate and are okay with occasional stutters
121-
# gc.collect()
117+
# from occasional garbage collection. However, garbage collection always
118+
# takes *some* time, so this will lower the average FPS. You can choose to
119+
# do this if you prefer a consistent frame rate, or don't if you prefer
120+
# maximum frame rate and are okay with occasional stutters gc.collect()
122121

123122
# For advanced users, you can use the internal buffers of the camera and
124123
# display drivers: `camera._buffer` and `display._buffer`. Using these
125124
# buffers directly can avoid the colorspace conversions implemented in
126125
# `camera.read()` and `display.imshow()`, which can improve performance if
127-
# your application can make use of the native colorspaces and improve
126+
# your application can make use of the native color spaces and improve
128127
# overall performance
129128

130129
# Check for key presses

examples/xrp_examples/ex02_grab_orange_ring.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,20 @@ def find_orange_ring_pipeline(frame):
5555
# Value: Anything above 30 is bright enough
5656
lower_bound = (15, 50, 30)
5757
upper_bound = (25, 255, 255)
58-
inRange = cv.inRange(hsv, lower_bound, upper_bound)
58+
in_range = cv.inRange(hsv, lower_bound, upper_bound)
5959

6060
# Noise in the image often causes `cv.inRange()` to return false positives
6161
# and false negatives, meaning there are some incorrect pixels in the binary
6262
# image. These can be cleaned up with morphological operations, which
6363
# effectively grow and shrink regions in the binary image to remove tiny
6464
# blobs of noise
6565
kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))
66-
morphOpen = cv.morphologyEx(inRange, cv.MORPH_OPEN, kernel)
67-
morphClose = cv.morphologyEx(morphOpen, cv.MORPH_CLOSE, kernel)
66+
morph_open = cv.morphologyEx(in_range, cv.MORPH_OPEN, kernel)
67+
morph_close = cv.morphologyEx(morph_open, cv.MORPH_CLOSE, kernel)
6868

6969
# Now we use `cv.findContours()` to find the contours in the binary image,
7070
# which are the boundaries of the regions in the binary image
71-
contours, hierarchy = cv.findContours(morphClose, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
71+
contours, hierarchy = cv.findContours(morph_close, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
7272

7373
# It's possible that no contours were found, so first check if any were
7474
# found before proceeding
@@ -91,7 +91,7 @@ def find_orange_ring_pipeline(frame):
9191

9292
# If no contour was found, return invalid values to indicate that
9393
if best_contour is None:
94-
return (-1, -1)
94+
return -1, -1
9595

9696
# Calculate the bounding rectangle of the contour, and use that to calculate
9797
# the center coordinates of the ring
@@ -141,7 +141,7 @@ def find_orange_ring_pipeline(frame):
141141

142142
# Now we can return the distance and position of the ring in cm, since
143143
# that's the only data we need from this pipeline
144-
return (distance_cm, position_x_cm)
144+
return distance_cm, position_x_cm
145145

146146
# Move the servo out of the way of the camera
147147
servo_one.set_angle(90)
@@ -157,7 +157,7 @@ def find_orange_ring_pipeline(frame):
157157
while True:
158158
# Read a frame from the camera
159159
success, frame = camera.read()
160-
if success == False:
160+
if not success:
161161
print("Error reading frame from camera")
162162
break
163163

0 commit comments

Comments
 (0)