pref: clean code format
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
from maix import image, time
|
from maix import image, time
|
||||||
|
|
||||||
from camera_manager import camera_manager
|
from camera_manager import camera_manager
|
||||||
|
|
||||||
_USE_CV = False
|
_USE_CV = False
|
||||||
try:
|
try:
|
||||||
import cv2
|
import cv2
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
_USE_CV = True
|
_USE_CV = True
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
@@ -45,7 +47,8 @@ def find_ellipse(img_cv, cx, cy, roi_r, th, ratio):
|
|||||||
(ex, ey), (ew, eh), ang = cv2.fitEllipse(cnt)
|
(ex, ey), (ew, eh), ang = cv2.fitEllipse(cnt)
|
||||||
mask_ellipse = np.zeros((HEIGHT, WIDTH), dtype=np.uint8)
|
mask_ellipse = np.zeros((HEIGHT, WIDTH), dtype=np.uint8)
|
||||||
cv2.ellipse(mask_ellipse, (int(ex), int(ey)), (int(ew / 2), int(eh / 2)), ang, 0, 360, 255, -1)
|
cv2.ellipse(mask_ellipse, (int(ex), int(ey)), (int(ew / 2), int(eh / 2)), ang, 0, 360, 255, -1)
|
||||||
brightness = img_cv[:, :, 0].astype(np.int32) + img_cv[:, :, 1].astype(np.int32) + img_cv[:, :, 2].astype(np.int32)
|
brightness = img_cv[:, :, 0].astype(np.int32) + img_cv[:, :, 1].astype(np.int32) + img_cv[:, :, 2].astype(
|
||||||
|
np.int32)
|
||||||
masked = np.where(mask_ellipse > 0, brightness, 0)
|
masked = np.where(mask_ellipse > 0, brightness, 0)
|
||||||
vals = masked[masked > 0]
|
vals = masked[masked > 0]
|
||||||
if len(vals) > 0:
|
if len(vals) > 0:
|
||||||
@@ -78,26 +81,33 @@ def find_brightest_bytes(frame, cx, cy, roi_r, th, ratio):
|
|||||||
for y in range(y1, y2, 2):
|
for y in range(y1, y2, 2):
|
||||||
for x in range(x1, x2, 2):
|
for x in range(x1, x2, 2):
|
||||||
idx = (y * WIDTH + x) * 3
|
idx = (y * WIDTH + x) * 3
|
||||||
r = data[idx]; g = data[idx+1]; b = data[idx+2]
|
r = data[idx];
|
||||||
|
g = data[idx + 1];
|
||||||
|
b = data[idx + 2]
|
||||||
if (r > th and r > g * ratio and r > b * ratio) or \
|
if (r > th and r > g * ratio and r > b * ratio) or \
|
||||||
(r > 200 and g > 200 and b > 200 and r >= g and r >= b and (r - g) > 10 and (r - b) > 10):
|
(r > 200 and g > 200 and b > 200 and r >= g and r >= b and (r - g) > 10 and (r - b) > 10):
|
||||||
score = r + g + b
|
score = r + g + b
|
||||||
dx = x - cx; dy = y - cy
|
dx = x - cx;
|
||||||
score *= max(0.5, 1.0 - ((dx*dx + dy*dy) ** 0.5 / roi_r) * 0.5)
|
dy = y - cy
|
||||||
|
score *= max(0.5, 1.0 - ((dx * dx + dy * dy) ** 0.5 / roi_r) * 0.5)
|
||||||
if score > best_score:
|
if score > best_score:
|
||||||
best_score = score
|
best_score = score
|
||||||
best_pos = (x, y)
|
best_pos = (x, y)
|
||||||
if best_pos is None:
|
if best_pos is None:
|
||||||
return None
|
return None
|
||||||
fx, fy = best_pos
|
fx, fy = best_pos
|
||||||
x1f = max(0, fx - 3); x2f = min(WIDTH, fx + 4)
|
x1f = max(0, fx - 3);
|
||||||
y1f = max(0, fy - 3); y2f = min(HEIGHT, fy + 4)
|
x2f = min(WIDTH, fx + 4)
|
||||||
|
y1f = max(0, fy - 3);
|
||||||
|
y2f = min(HEIGHT, fy + 4)
|
||||||
best_bright = 0
|
best_bright = 0
|
||||||
final_pos = best_pos
|
final_pos = best_pos
|
||||||
for y in range(y1f, y2f):
|
for y in range(y1f, y2f):
|
||||||
for x in range(x1f, x2f):
|
for x in range(x1f, x2f):
|
||||||
idx = (y * WIDTH + x) * 3
|
idx = (y * WIDTH + x) * 3
|
||||||
r = data[idx]; g = data[idx+1]; b = data[idx+2]
|
r = data[idx];
|
||||||
|
g = data[idx + 1];
|
||||||
|
b = data[idx + 2]
|
||||||
if (r > th and r > g * ratio and r > b * ratio) or \
|
if (r > th and r > g * ratio and r > b * ratio) or \
|
||||||
(r > 200 and g > 200 and b > 200 and r >= g and r >= b and (r - g) > 10 and (r - b) > 10):
|
(r > 200 and g > 200 and b > 200 and r >= g and r >= b and (r - g) > 10 and (r - b) > 10):
|
||||||
rgb_sum = r + g + b
|
rgb_sum = r + g + b
|
||||||
@@ -108,7 +118,6 @@ def find_brightest_bytes(frame, cx, cy, roi_r, th, ratio):
|
|||||||
|
|
||||||
|
|
||||||
def get_stable_laser_point(timeout_ms=15000, stable_count=STABLE_COUNT):
|
def get_stable_laser_point(timeout_ms=15000, stable_count=STABLE_COUNT):
|
||||||
own_cam = False
|
|
||||||
try:
|
try:
|
||||||
last_pos = None
|
last_pos = None
|
||||||
stable = 0
|
stable = 0
|
||||||
@@ -139,8 +148,4 @@ def get_stable_laser_point(timeout_ms=15000, stable_count=STABLE_COUNT):
|
|||||||
return (int(pos[0]), int(pos[1]))
|
return (int(pos[0]), int(pos[1]))
|
||||||
time.sleep_ms(500)
|
time.sleep_ms(500)
|
||||||
finally:
|
finally:
|
||||||
if own_cam:
|
|
||||||
try:
|
|
||||||
cam.close()
|
|
||||||
except:
|
|
||||||
pass
|
pass
|
||||||
|
|||||||
Reference in New Issue
Block a user