forked from openmv/openmv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpc_popular_features_as_the_controller_device_example.py
165 lines (143 loc) · 6.12 KB
/
rpc_popular_features_as_the_controller_device_example.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# Remote Control - As The Controller Device
#
# This script remotely controls an OpenMV Cam using the RPC library.
#
# This script is meant to talk to the "popular_features_as_the_remote_device.py" on the OpenMV Cam.
import json, rpc, serial, serial.tools.list_ports, struct, sys
from datetime import datetime
# Fix Python 2.x.
try: input = raw_input
except NameError: pass
##############################################################
# Choose the interface you wish to control an OpenMV Cam over.
##############################################################
# Uncomment the below lines to setup your OpenMV Cam for controlling over a USB VCP.
#
# * port - Serial Port Name.
#
print("\nAvailable Ports:\n")
for port, desc, hwid in serial.tools.list_ports.comports():
print("{} : {} [{}]".format(port, desc, hwid))
sys.stdout.write("\nPlease enter a port name: ")
sys.stdout.flush()
interface = rpc.rpc_usb_vcp_master(port=input())
print("")
sys.stdout.flush()
# Uncomment the below line to setup your OpenMV Cam for controlling over WiFi.
#
# * slave_ip - IP address to connect to.
# * my_ip - IP address to bind to ("" to bind to all interfaces...)
# * port - Port to route traffic to.
#
# interface = rpc.rpc_network_master(slave_ip="xxx.xxx.xxx.xxx", my_ip="", port=0x1DBA)
# Uncomment the below lines to setup your OpenMV Cam for controlling over a UART (Serial / COM Port).
#
# * port - Serial Port Name.
# * baudrate - Bits per second.
#
# print("\nAvailable Ports:\n")
# for port, desc, hwid in serial.tools.list_ports.comports():
# print("{} : {} [{}]".format(port, desc, hwid))
# sys.stdout.write("\nPlease enter a port name: ")
# sys.stdout.flush()
# interface = rpc.rpc_uart_master(port=input(), baudrate=115200)
# print("")
# sys.stdout.flush()
# Uncomment the below line to setup your OpenMV Cam for controlling over CAN.
#
# * channel - Kvarser CAN channel number.
# * message_id - 11-bit message ID to use for data transport.
# * bit_rate - CAN baud rate.
# * sampling_point - Sampling point percentage.
#
# from canlib import canlib
# print("\nAvailable Channels:\n")
# for channel in range(canlib.getNumberOfChannels()):
# chdata = canlib.ChannelData(channel)
# print("%d. %s (%s / %s)" % (channel, chdata.channel_name, chdata.card_upc_no, chdata.card_serial_no))
# sys.stdout.write("\nPlease enter a channel name: ")
# sys.stdout.flush()
# interface = rpc.rpc_kvarser_can_master(channel=int(input()), message_id=0x7FF, bit_rate=250000, sampling_point=75)
# print("")
# sys.stdout.flush()
##############################################################
# Call Back Handlers
##############################################################
def exe_face_detection():
result = interface.call("face_detection")
if result is not None and len(result):
print("Largest Face Detected [x=%d, y=%d, w=%d, h=%d]" % struct.unpack("<HHHH", result))
def exe_person_detection():
result = interface.call("person_detection")
if result is not None:
print(result.tobytes())
def exe_qrcode_detection():
result = interface.call("qrcode_detection")
if result is not None and len(result):
print(result.tobytes())
def exe_all_qrcode_detection():
result = interface.call("all_qrcode_detection")
if result is not None and len(result):
print("QR Codes Detected:")
for obj in json.loads(result.tobytes()):
print(obj)
def exe_apriltag_detection():
result = interface.call("apriltag_detection")
if result is not None and len(result):
print("Largest Tag Detected [cx=%d, cy=%d, id=%d, rot=%d]" % struct.unpack("<HHHH",result))
def exe_all_apriltag_detection():
result = interface.call("all_apriltag_detection")
if result is not None and len(result):
print("Tags Detected:")
for obj in json.loads(result.tobytes()):
print(obj)
def exe_datamatrix_detection():
result = interface.call("datamatrix_detection")
if result is not None and len(result):
print(result.tobytes())
def exe_all_datamatrix_detection():
result = interface.call("all_datamatrix_detection")
if result is not None and len(result):
print("Data Matrices Detected:")
for obj in json.loads(result.tobytes()):
print(obj)
def exe_barcode_detection():
result = interface.call("barcode_detection")
if result is not None and len(result):
print(result.tobytes())
def exe_all_barcode_detection():
result = interface.call("all_barcode_detection")
if result is not None and len(result):
print("Bar Codes Detected:")
for obj in json.loads(result.tobytes()):
print(obj)
def exe_color_detection():
thresholds = (30, 100, 15, 127, 15, 127) # generic red thresholds
# thresholds = (30, 100, -64, -8, -32, 32) # generic green thresholds
# thresholds = (0, 30, 0, 64, -128, 0) # generic blue thresholds
result = interface.call("color_detection", struct.pack("<bbbbbb", *thresholds))
if result is not None and len(result):
print("Largest Color Detected [cx=%d, cy=%d]" % struct.unpack("<HH", result))
def exe_jpeg_snapshot():
result = interface.call("jpeg_snapshot")
if result is not None:
name = "snapshot-%s.jpg" % datetime.now().strftime("%d.%m.%Y-%H.%M.%S")
print("Writing jpeg %s..." % name)
with open(name, "wb") as snap:
snap.write(result)
# Execute remote functions in a loop. Please choose and uncomment one remote function below.
# Executing multiple at a time may run slowly if the camera needs to change camera modes
# per execution.
while(True):
exe_face_detection() # Face should be about 2ft away.
# exe_person_detection()
# exe_qrcode_detection() # Place the QRCode about 2ft away.
# exe_all_qrcode_detection() # Place the QRCode about 2ft away.
# exe_apriltag_detection()
# exe_all_apriltag_detection()
# exe_datamatrix_detection() # Place the Datamatrix about 2ft away.
# exe_all_datamatrix_detection() # Place the Datamatrix about 2ft away.
# exe_barcode_detection() # Place the Barcode about 2ft away.
# exe_all_barcode_detection() # Place the Barcode about 2ft away.
# exe_color_detection()
# exe_jpeg_snapshot()