-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_build_dataset.py
68 lines (61 loc) · 1.99 KB
/
04_build_dataset.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
import numpy as np
import serial
import sys
import uuid
from PIL import Image, ImageDraw
port = '/dev/ttyACM0'
baudrate = 115600
# Initialize serial port
ser = serial.Serial()
ser.port = port
ser.baudrate = baudrate
ser.open()
ser.reset_input_buffer()
label = ""
width = 160
height = 120
num_ch = 3
# creating 3-dim tuple image without initializing entries with empty
image = np.empty((height, width, num_ch), dtype=np.uint8)
def serial_readline():
data = ser.readline() # read a '\n' terminated line
return data.decode("utf-8").strip()
while True:
data_str = serial_readline()
if str(data_str) == "<image>":
w_str = serial_readline()
h_str = serial_readline()
w = int(w_str)
h = int(h_str)
if w != width or h != height:
print("Resizing numpy array")
if w * h != width * height:
image.resize((h, w, num_ch))
else:
image.reshape((h, w, num_ch))
width = w
height = h
print("Reading frame:", width, height)
for i in range(0, width * height * num_ch):
c = int(i % num_ch)
y = int((i / num_ch) / width)
x = int((i / num_ch) % width)
data_str = serial_readline()
image[y][x][c] = int(data_str)
data_str = serial_readline()
if str(data_str) == "</image>":
crop_area = (0, 0, height, height)
image_pil = Image.fromarray(image)
image_cropped = image_pil.crop(crop_area)
draw = ImageDraw.Draw(image_cropped)
label = serial_readline()
print(label)
draw.rectangle((20, 20, 100, 100), outline="red")
draw.text((0, 100), label)
image_cropped.show()
unique_id = str(uuid.uuid4())
filename = label + "_"+ unique_id + ".png"
image_cropped.save(filename)
print("File", filename, "saved")
else:
print("Error capture image")