-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
89 lines (76 loc) · 2.05 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import sensor
import time
import math
from pyb import UART
import pyb
thresholds = [
(21, 72, 15, 81, -21, 51), # generic_red_thresholds
(0, 100, -128, 125, -88, -24), # generic_green_thresholds
(6, 22, 3, 19, -49, -28),
] # generic_blue_thresholds
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)
sensor.set_auto_gain(False) # 关闭自动增益
sensor.set_auto_whitebal(False) # 关闭自动白平衡
ser = UART(3,19200)
clock = time.clock()
ld1 = pyb.LED(1)
ld2 = pyb.LED(2)
ld3 = pyb.LED(3)
def detect_color(_index):
while True:
ld1.on()
ld2.on()
ld3.on()
clock.tick()
img = sensor.snapshot()
for blob in img.find_blobs(
[thresholds[_index]],
pixels_threshold=2000,
area_threshold=2000,
merge=True
):
# These values depend on the blob not being circular - otherwise they will be shaky.
img.draw_rectangle(blob.rect())
# 绘制中心十字
img.draw_cross(blob.cx(), blob.cy())
# 绘制中心圈
img.draw_keypoints(
[(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20
)
# print(blob.rect())
if blob.rect() != None:
ld1.off()
# ld2.off()
ld3.off()
return 1
# print(1)
def QR_detect():
while True:
ld1.on()
ld2.on()
ld3.on()
img = sensor.snapshot()
img.lens_corr(1.8)
for data in img.find_qrcodes():
return data.payload(),1
time.sleep_ms(25)
ld1.off()
ld2.off()
ld3.off()
time.sleep_ms(25)
def color_choice(_sign):
if _sign == '11':
return 0
elif _sign == '22':
return 1
elif _sign == '33':
return 2
info, sign = QR_detect()
print(info)
index = color_choice(info)
#while (detect_color(index) == 1):
# ld2.on()
print(detect_color(index))