Data collection

2024. 6. 27. 11:04Jetson

 

✅ 신호등 데이터 수집을 위한 활동

from jetbot import Camera, bgr8_to_jpeg
from IPython.display import display

import ipywidgets.widgets as widgets
import traitlets

camera = Camera.instance(width = 224, height = 224)
image = widgets.Image(format = 'jpeg', width = 224, height = 224)

camera_link = traitlets.dlink((camera, 'value'), (image, 'value'), transform = bgr8_to_jpeg)

display(image)

camera_link.unlink() # 카메라 꺼두기
# 데이터를 저장하기 위해 red, green 데이터가 들어갈 폴더를 생성

import os

red_dir = 'dataset/red' # red 폴더
green_dir = 'dataset/green' # green 폴더

try :
    os.makedirs(red_dir)
    os.makedirs(green_dir)
    
except FileExistsError :
    print("파일이 이미 존재합니다.")

# 빨간색, 초록색 버튼을 누르면
# 몇번 눌렸는지 지금까지 수집한 데이터 갯수를 알려주는 텍스트 박수
# danger - 빨강, warning - 주황, info - 파랑, success - 초록

button_layout = widgets.Layout(width = '128px', height = '30px') # 버튼 사이즈 설정

red_button = widgets.Button(description = 'red data', button_style = 'danger', layout = button_layout)
red_count = widgets.IntText(layout = button_layout, value = len(os.listdir(red_dir)))

green_button = widgets.Button(description = 'green data', button_style = 'success', layout = button_layout)
green_count = widgets.IntText(layout = button_layout, value = len(os.listdir(green_dir)))

# display(red_button)
# display(red_count)

# display(green_button)
# display(green_count)

display(widgets.HBox([red_count, red_button])) # 수평선에 버튼, 텍스트 박스 위치
display(widgets.HBox([green_count, green_button]))

from uuid import uuid1 # uuid1 -> 현재 시간과 네트워크 주소를 기반으로 고유 식별자를 생성!
# 고유 식별자 만드는 라이브러리
# Universally Unique Identifier, UUID -> uuid1, uuid2, uuid3, ...

def save_snapshot(directory) :
    image_path = os.path.join(directory, str(uuid1()) + '.jpg')
    with open(image_path, 'wb') as f :
        f.write(image.value)
        
def save_red() :
    global red_dir, red_count
    save_snapshot(red_dir)
    red_count.value = len(os.listdir(red_dir))

def save_green() :
    global green_dir, green_count
    save_snapshot(green_dir)
    green_count.value = len(os.listdir(green_dir))
    
def on_click_save_red(x) :
    save_red()
    
def on_click_save_green(x) :
    save_green()
    
red_button.on_click(on_click_save_red)
green_button.on_click(on_click_save_green)

 

'Jetson' 카테고리의 다른 글

LCD 패널 제어  (0) 2024.07.01
Widget Button  (1) 2024.06.28
Wireless_Joystick  (0) 2024.06.24
JoyStick  (0) 2024.06.21
Face Tracking  (1) 2024.06.21