관리 메뉴

커리까지

농구영상 득점 자동 판별기 만들기 02 본문

프로젝트/농구 득점 영상 딥러닝 만들기

농구영상 득점 자동 판별기 만들기 02

목표는 커리 2021. 2. 14. 18:12
728x90
SMALL

가상환경 만들고 다시 설정

(base) C:\Windows\system32>conda create --name deep
  • 가상환경 이름을 deep으로 주었다.
(base) C:\Windows\system32>conda activate deep
  • 가상환경을 실행하였다.
(deep) C:\Windows\system32>conda install tensorflow-gpu==1.12
  • 텐서플로우를 설치하였다.
(deep) C:\Windows\system32>conda install jupyter notebook
  • 주피터 노트북을 설치하였다.
(deep) C:\Windows\system32>conda install pandas
(deep) C:\Windows\system32>conda install numpy
  • 판다스랑 넘파이도 설치해준다.

07

  • 흐어어어어어엉 ㅠㅠ 드디어 gpu가 잡힌다. 외부모니터만 해제하면 잡히는걸 다른거 깔고 지우고 다른 길로 돌아 돌아 갔다.

darkflow에 필요한 패키지 conda 설치

conda install matplotlib
conda install -c conda-forge opencv
conda install cython
conda install -c hellock icrawler
pip install opencv-python
conda install -c conda-forge tensorflow=1.14
conda install -c anaconda tensorflow-gpu=1.14

구동성공

  • 드디어 gpu로 구동하였다. 역시 cpu보다 빠르다. 그러나 gpu사양이 그렇게 좋은건 아니라서 정상적인 속도는 아니지만 그래도 만족한다.
  • 이게 실시간으로 보면 느려져서 차리리 저장하였다. 그랬더니 원래 속도로 결과를 보여준다.
import cv2
from darkflow.net.build import TFNet
import numpy as np
import time

options = {
    'model': 'cfg/yolo.cfg',
    'load': 'bin/yolo.weights',
    'threshold': 0.2,
    'gpu': 1.0
}

tfnet = TFNet(options)
colors = [tuple(255 * np.random.rand(3)) for _ in range(10)]


# capture = cv2.VideoCapture(0)
capture = cv2.VideoCapture('nba_sample.mp4')
capture.set(cv2.CAP_PROP_FRAME_WIDTH,1280)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
# capture.set(cv2.CAP_PROP_FPS, int(60))

width = capture.get(cv2.CAP_PROP_FRAME_WIDTH)
height = capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
out = cv2.VideoWriter('output.avi', fourcc, 30.0, (int(width), int(height)))

while True:
    stime = time.time()
    ret, frame = capture.read()
    if ret:
        results = tfnet.return_predict(frame)
        for color, result in zip(colors, results):
            tl = (result['topleft']['x'], result['topleft']['y'])
            br = (result['bottomright']['x'], result['bottomright']['y'])
            label = result['label']
            confidence = result['confidence']
            text = '{}: {:.0f}%'.format(label, confidence * 100)
            frame = cv2.rectangle(frame, tl, br, color, 2)
            frame = cv2.putText(
                frame, text, tl, cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0), 2)
        cv2.imshow('frame', frame)
        out.write(frame)
        print('FPS {:.1f}'.format(1 / (time.time() - stime)))
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

capture.release()
out.release()
cv2.destroyAllWindows()

추가한 부분

width = capture.get(cv2.CAP_PROP_FRAME_WIDTH)
height = capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
out = cv2.VideoWriter('output.avi', fourcc, 30.0, (int(width), int(height)))
 out.write(frame)
out.release()
  • 이렇게 3개 추가하였다.

08

  • 이렇게 원래 속도로 인식해서 보여준다. 속이 다 시원하다^^
  • 코트 위에 있는 선수들을 인식하게하고 라벨의 크기를 조절하려고 한다.

텍스트 라벨 수정

import cv2
from darkflow.net.build import TFNet
import numpy as np
import time
from PIL import ImageFont, ImageDraw, Image


font = ImageFont.truetype("./fontzip/NanumSquareB.ttf", 10)


options = {
    'model': 'cfg/yolo.cfg',
    'load': 'bin/yolo.weights',
    'threshold': 0.2,
    'gpu': 1.0
}

tfnet = TFNet(options)
colors = [tuple(255 * np.random.rand(3)) for _ in range(10)]


# capture = cv2.VideoCapture(0)
capture = cv2.VideoCapture('nba_sample.mp4')
capture.set(cv2.CAP_PROP_FRAME_WIDTH,1280)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
# capture.set(cv2.CAP_PROP_FPS, int(60))

width = capture.get(cv2.CAP_PROP_FRAME_WIDTH)
height = capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
out = cv2.VideoWriter('output.avi', fourcc, 30.0, (int(width), int(height)))

prev_time = 0
FPS = 10

while True:
    # stime = time.time()
    ret, frame = capture.read()
    current_time = time.time() - prev_time
    if(ret is True) and (current_time > 1./ FPS):
        prev_time = time.time()
        results = tfnet.return_predict(frame)
        for color, result in zip(colors, results):
            tl = (result['topleft']['x'], result['topleft']['y'])
            br = (result['bottomright']['x'], result['bottomright']['y'])
            label = result['label']
            confidence = result['confidence']
            text = '{}: {:.0f}%'.format(label, confidence * 100)
            frame = cv2.rectangle(frame, tl, br, color, 2)

            text_size = cv2.getTextSize(text=text, fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1, thickness=2)
            text_width, text_height = text_size[0][0], text_size[0][1]
            cv2.rectangle(frame, pt1=(result['topleft']['x']-1, result['topleft']['y'] - text_height), pt2=(result['topleft']['x']-30 + text_width, result['topleft']['y']), color=color, thickness=-1)
            frame = cv2.putText(
                frame, text, (result['topleft']['x'], result['topleft']['y']-4), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
        cv2.imshow('frame', frame)
        out.write(frame)
        print('FPS {:.1f}'.format(1 / (time.time() - prev_time)))
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

capture.release()
out.release()
cv2.destroyAllWindows()

추가한 부분

from PIL import ImageFont, ImageDraw, Image

font = ImageFont.truetype("./fontzip/NanumSquareB.ttf", 10)

text_size = cv2.getTextSize(text=text, fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=1, thickness=2)
text_width, text_height = text_size[0][0], text_size[0][1]
cv2.rectangle(frame, pt1=(result['topleft']['x']-1, result['topleft']['y'] - text_height), pt2=(result['topleft']['x']-30 + text_width, result['topleft']['y']), color=color, thickness=-1)
  • 우선 font가 마음에 들지 않아 바꿨고 텍스트의 크기를 받아서 박스의 크기를 설정해준다.

수정한 부분

frame = cv2.putText(
                frame, text, (result['topleft']['x'], result['topleft']['y']-4), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
  • 텍스트의 위치를 조금 수정하였다. y좌표가 밑으로 쳐지는 경향이 있어서 박스안에 들어오게 바꾸었다. 글자색도 흰색으로 변경하였고 크키고 글씨 크기도 조금 줄였다.

09

  • 저번보다 보기에 편해졌다. 글씨체좀 동글동글하게 바꾸고 해상도도 바꿔야겠다.
  • 더 찾아봐야겠다. 이미지 예시는 있는데 puttext는 안보여서 더 찾아보려고 한다.
  • 그 다음에는 코트위 선수들을 어떻게 인식할 지 찾아봐야겠다.

oepn source basketball yolo code

10

  • 구글에서 검색했을때 나오는 결과중 3개의 코드를 분석해서 실행하려고 한다. 아주 자세히 나와있고 잘 인식해준다. 보니깐 우선 코트를 학습하고 사람의 관절의 움직임을 분석한다. 신기했다. 이 코드를 공부하고 또 실행하려고 한다. 딥러닝의 세계는 신기하다.
728x90
LIST
Comments