YOLOv8 핸드폰 카메라로 실시간 사물 인식 (Android Webcam) (5)

YOLOv8 핸드폰 카메라로 실시간 사물 인식을 직접 webrtc를 이용해서 서버를 구축하려 했지만… 취미로 하는 초보 코딩의 한계로 어렵네요… 이번에는 간단하게 IP Webcam App 을 이용해 보겠습니다.

YOLOv8 핸드폰 카메라로 실시간 사물 인식

핸드폰 카메라 영상 전송

YOLOv8 핸드폰 카메라로 실시간 사물을 실시간으로 핸드폰 카메라로 영상을 전송 시켜주기 위해 IP Webcam App 설치 어플을 이용해서 비디오 영상을 전송해 줍니다.

어플은 스토어 쉽게 찾을 수 있습니다. 설치하여 실행하면 아래와 같이 서버 시작 버튼을 찾을 수 있어요

실행하면 이렇게 ip 번호가 나옵니다.

생성된 ip 번호를 웹브라우저에서 입력하여 들어가면 아래와 같이 화면이 공유 됩니다.

핸드폰 카메라 영상 전송

핸드폰의 카메라를 통해 영상을 yolov8로 전달하기 위한 영상의 주소는 아래와 같이 확인합니다.

크롬브라우저 개발자도구 F12를 눌러 화면의 주소를 확인하면


아래와 같이 영상의 주소를 알 수 있어요.

저 같은 경우 http://192.168.50.145:8080/video 이렇게 되어 있네요.

핸드폰 카메라를 이용해 YOLOv8 실시간 영상 인식

IP webcam 어플로 생성한 영상 주소를 이용하여 핸드폰 카메라를 이용해 YOLOv8 실시간 영상 인식을 하기위해선

python에서 아래와 같이 실행합니다.

from ultralytics import YOLO
import cv2
import math
cap=cv2.VideoCapture("http://192.168.50.145:8080/video")

frame_width=int(cap.get(3))
frame_height = int(cap.get(4))

out=cv2.VideoWriter('output.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (frame_width, frame_height))

model=YOLO("../YOLO-Weights/yolov8n.pt")
classNames = ["person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck", "boat",
              "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat",
              "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella",
              "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat",
              "baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup",
              "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli",
              "carrot", "hot dog", "pizza", "donut", "cake", "chair", "sofa", "pottedplant", "bed",
              "diningtable", "toilet", "tvmonitor", "laptop", "mouse", "remote", "keyboard", "cell phone",
              "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors",
              "teddy bear", "hair drier", "toothbrush"
              ]
while True:
    success, img = cap.read()
    # Doing detections using YOLOv8 frame by frame
    #stream = True will use the generator and it is more efficient than normal
    results=model(img,stream=True)
    #Once we have the results we can check for individual bounding boxes and see how well it performs
    # Once we have have the results we will loop through them and we will have the bouning boxes for each of the result
    # we will loop through each of the bouning box
    for r in results:
        boxes=r.boxes
        for box in boxes:
            x1,y1,x2,y2=box.xyxy[0]
            #print(x1, y1, x2, y2)
            x1,y1,x2,y2=int(x1), int(y1), int(x2), int(y2)
            print(x1,y1,x2,y2)
            cv2.rectangle(img, (x1,y1), (x2,y2), (255,0,255),3)
            #print(box.conf[0])
            conf=math.ceil((box.conf[0]*100))/100
            cls=int(box.cls[0])
            class_name=classNames[cls]
            label=f'{class_name}{conf}'
            t_size = cv2.getTextSize(label, 0, fontScale=1, thickness=2)[0]
            #print(t_size)
            c2 = x1 + t_size[0], y1 - t_size[1] - 3
            cv2.rectangle(img, (x1,y1), c2, [255,0,255], -1, cv2.LINE_AA)  # filled
            cv2.putText(img, label, (x1,y1-2),0, 1,[255,255,255], thickness=1,lineType=cv2.LINE_AA)
    out.write(img)
    cv2.imshow("Image", img)
    if cv2.waitKey(1) & 0xFF==ord('1'):
        break
out.release()

기존에 웹캠 부분인 0 을 위에서 추출한 영상주소를 입력하여 실행하면 됩니다.

cap=cv2.VideoCapture(0) ------------> 이부분을
cap=cv2.VideoCapture("http://192.168.50.145:8080/video") 본인의 영상 주소 입력

YOLOv8 핸드폰 카메라로 실시간 사물 인식을 해보았는데요 PC성능이 낮아 FPS가 좋진 않지만 그래도 작동은 잘하네요.

YOLOv8 핸드폰 카메라

커스텀된 학습 모델을만들어서 남는 핸드폰 공기계를 이용해 다양한 재밌는 작업들을 시도 해 볼 수 있을 것 같습니다. 예전에 사무실에 웹캠으로 상사를 인식하여 업무 시간에 딴 짓을 하다 자동으로 알트탭을 해주는 영상이 있었는데 그런 재밌는 것들을 시도 해 볼 수 있을 것 같습니다.ㅎㅎ

관련 영상은 지금은 못 찾겠네요…….

브라우저를 통해 영상을 전달하고 서버에서 결과를 다시 브라우저로 보내주는 코딩을 하고 있는데 너무 어렵네요… 혹시 관련 내용에 대해 잘 아시는 분 계시면 도와주세요 ㅠㅠ

다른 포스팅

댓글 남기기

%d 블로거가 이것을 좋아합니다: