-
Notifications
You must be signed in to change notification settings - Fork 0
/
opencv_openmv_bridge.py
76 lines (59 loc) · 2.14 KB
/
opencv_openmv_bridge.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
import numpy as np
import io
import struct
import serial
from PIL import Image as PILImage
import cv2
# Camera object to create the snaps/frames/images that
# will be deserialized later in the opencv code
class Camera:
def __init__(self, device='/dev/ttyACM0'):
"""Reads images from OpenMV Cam
Args:
device (str): Serial device
Raises:
serial.SerialException
"""
self.port = serial.Serial(device, baudrate=115200,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
xonxoff=False, rtscts=False,
stopbits=serial.STOPBITS_ONE,
timeout=None, dsrdtr=True)
# Important: reset buffers for reliabile restarts of OpenMV Cam
self.port.reset_input_buffer()
self.port.reset_output_buffer()
def read_image(self):
"""Read image from OpenMV Cam
Returns:
image (ndarray): Image
Raises:
serial.SerialException
"""
# Sending 'snap' command causes camera to take snapshot
self.port.write('snap')
self.port.flush()
# Read 'size' bytes from serial port
size = struct.unpack('<L', self.port.read(4))[0]
image_data = self.port.read(size)
image = np.array(PILImage.open(io.BytesIO(image_data)))
return image
currentFrame = 0
while(True):
# Create a camera by just giving the ttyACM depending on your connection value
# Change the following line depending on your connection
cap = Camera(device='/dev/ttyACM0')
# Capture frame-by-frame
im1 = cap.read_image()
# Our operations on the frame come here
gray = cv2.cvtColor(im1, cv2.COLOR_BGR2GRAY)
# Saves image of the current frame in jpg file
# name = 'frame' + str(currentFrame) + '.jpg'
# cv2.imwrite(name, frame)
# Display the resulting frame
cv2.imshow('im1',im1)
# cv2.imshow('im1',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# To stop duplicate images
currentFrame += 1