ch0nny_log

[빅데이터분석] 딥러닝_22. 영상판독기 본문

카테고리 없음

[빅데이터분석] 딥러닝_22. 영상판독기

chonny 2024. 11. 1. 10:54

데이터관련 교육 카페 | [우리팀 딥러닝 프로젝트 상용화 하기2] 우리팀 아이디어를 유료 제품화하기 ! - Daum 카페

 

Daum 카페

 

cafe.daum.net

 

!pip install deepface
!pip install ultralytics
import cv2
import tkinter as tk
from PIL import Image, ImageTk
import threading
from ultralytics import YOLO

# YOLO 모델 로드
model = YOLO('yolov8n.pt')

# tkinter 윈도우 설정
root = tk.Tk()
root.title("YOLOv8 실시간 사람 검출")

# 웹캠 캡처 설정
cap = cv2.VideoCapture(0)

# 사물 검출 상태를 저장하는 변수
detecting = False

# 웹캠 프레임을 업데이트하고 사람 검출하는 함수
def update_frame():
    global detecting
    ret, frame = cap.read()
    if ret:
        if detecting:
            # YOLO 모델로 사람 검출
            results = model(frame)
            for result in results:
                boxes = result.boxes.cpu().numpy()
                for box in boxes:
                    # 검출된 객체가 사람일 경우에만 표시 (label이 'person'인 경우)
                    if int(box.cls[0]) == 0:  # 0은 'person' 클래스 번호
                        x1, y1, x2, y2 = map(int, box.xyxy[0])
                        label = result.names[int(box.cls[0])]  # 클래스 ID를 이름으로 변환
                        # 사람 텍스트와 사각형 박스를 그리기
                        cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
                        cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)

        # OpenCV 이미지를 PIL 이미지로 변환
        img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        img = Image.fromarray(img)
        imgtk = ImageTk.PhotoImage(image=img)
        
        # 레이블에 이미지 업데이트
        video_label.imgtk = imgtk
        video_label.configure(image=imgtk)
    video_label.after(10, update_frame)

# 사물 검출 시작 및 중지 함수
def toggle_detection():
    global detecting
    detecting = not detecting
    if detecting:
        detection_button.config(text="사물 검출 중지")
        print("사물 검출 시작")
    else:
        detection_button.config(text="사물 검출 시작")
        print("사물 검출 중지")

# 비디오 레이블 설정
video_label = tk.Label(root)
video_label.pack()

# 사물 검출 버튼 설정
detection_button = tk.Button(root, text="사물 검출 시작", command=toggle_detection)
detection_button.pack(padx=10, pady=10)

# 프레임 업데이트 시작
update_frame()

# tkinter 윈도우 실행
root.mainloop()

# 프로그램 종료 시 웹캠 해제
cap.release()

 

 


홈런을 부르는 타격폼, 인공지능은 알고있다

 

1. 아래의 코드를 메모장에 .py 파일로 저장

import av
import cv2
import streamlit as st
from streamlit_webrtc import webrtc_streamer
from ultralytics import YOLO

# 사용자 제공 모델 파일 경로 설정
model_path = 'hitter_trained_model.pt'

# YOLO 모델 로드
model = YOLO(model_path)

st.title("Real-Time Batter Recognition with YOLOv8 and WebRTC")

# 프레임 처리 콜백 함수
def video_frame_callback(frame):
    img = frame.to_ndarray(format="bgr24")
    
    # YOLO 모델을 사용하여 타자 감지 수행
    results = model(img)
    
    # 결과에서 각 타자의 바운딩 박스를 그립니다
    for result in results[0].boxes:
        x1, y1, x2, y2 = map(int, result.xyxy[0])  # 바운딩 박스 좌표
        label = result.cls[0]  # 클래스 라벨 (타자 이름)
        confidence = result.conf[0]  # 신뢰도 점수
        
        # 클래스 이름 얻기 (타자 이름)
        class_name = model.names[int(label)]
        
        # 특정 타자일 경우 또는 높은 신뢰도일 경우 바운딩 박스와 라벨 그리기
        if confidence > 0.5:
            cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
            cv2.putText(img, f"{class_name} {confidence:.2f}", (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
    
    # BGR에서 RGB로 변환 (Streamlit에서 이미지를 표시하기 위해)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    return av.VideoFrame.from_ndarray(img, format="rgb24")

# WebRTC 스트리밍 시작
webrtc_streamer(key="example", video_frame_callback=video_frame_callback)

homerun.py
0.00MB

 

 

 

 

2. c드라이브 data 파일에 넣기

hitter_trained_model.pt
5.95MB

 

 

 

 

 

실시간 영상 사물검출하는 영상판독기 만들기

 

실시간 영상 사물검출하는 영상판독기 만들기 | Notion

◾웹캠 사용하여 실시간 영상 출력하는 인터페이스 만들기

aluminum-magpie-a29.notion.site

우리팀 아이디어를 유료화하기 (영상판독기)

 

우리팀 아이디어를 유료화하기 (영상판독기) | Notion

우리팀 아이디어를 유료화하기 위한 제품화 1단계 - 실시간 영상속 사물검출하여 

gabby-nurse-0c2.notion.site