코딩일지/TIL: Today I Learned

20221013 TIL

야언 2022. 10. 13. 19:25

오늘의 한 일

  • YOLO5v 쪽지시험 테스트
  • 실전 머신러닝 적용 강의 - 복습 
  • github 탐방

 

YOLO5v 쪽지시험 테스트

 

 

일단 YOLO를 돌릴 시 이미지에서 다섯명의 박스가 생길 것이라 유추해볼 수 있다.

 

 

import torch
import cv2
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
imgs = ['Untitled.jpeg']  # batch of images

results = model(imgs)

results.save()  # or .show()

yolo를 돌려서 run - exp 폴더에서 확인해보면

 

 

 

5개의 person 박스와 한개의 tie박스가 인식되는것을 확인할 수 있다.

 

tie는 프로그램이 잘못 인식했다지만 솔직히 헷갈릴만 하다 싶네 ㅋㅋ

 

이 results 모델을 기반으로 각 사각형을 numpy 어레이로 변형시켜준 뒤 입맛대로 작업해주면 완료

import torch
import cv2
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)

img = cv2.imread('Untitled.jpeg')
results = model(img)
results.save()

result = results.pandas().xyxy[0].to_numpy()
result = [item for item in result if item[6]=='person']

tmp_img = cv2.imread('Untitled')

print(tmp_img.shape)  # 이미지의 가로, 세로 pixel

cropped1 = tmp_img[int(result[0][1]):int(result[0][3]), int(result[0][0]):int(result[0][2])]
cropped2 = tmp_img[int(result[1][1]):int(result[1][3]), int(result[1][0]):int(result[1][2])]
cropped3 = tmp_img[int(result[2][1]):int(result[2][3]), int(result[2][0]):int(result[2][2])]
cropped4 = tmp_img[int(result[3][1]):int(result[3][3]), int(result[3][0]):int(result[3][2])]
cropped5 = tmp_img[int(result[4][1]):int(result[4][3]), int(result[4][0]):int(result[4][2])]

cv2.imwrite('person1.png', cropped1)
cv2.imwrite('person2.png', cropped2)
cv2.imwrite('person3.png', cropped3)
cv2.imwrite('person4.png', cropped4)
cv2.imwrite('person5.png', cropped5)

cv2.rectangle(tmp_img, (int(results.xyxy[0][0][0].item()), int(results.xyxy[0][0][1].item())), (int(results.xyxy[0][0][2].item()), int(results.xyxy[0][0][3].item())), (255,255,255))
cv2.rectangle(tmp_img, (int(results.xyxy[0][1][0].item()), int(results.xyxy[0][1][1].item())), (int(results.xyxy[0][1][2].item()), int(results.xyxy[0][1][3].item())), (255,255,255))
cv2.rectangle(tmp_img, (int(results.xyxy[0][2][0].item()), int(results.xyxy[0][2][1].item())), (int(results.xyxy[0][2][2].item()), int(results.xyxy[0][2][3].item())), (255,255,255))
cv2.rectangle(tmp_img, (int(results.xyxy[0][3][0].item()), int(results.xyxy[0][3][1].item())), (int(results.xyxy[0][3][2].item()), int(results.xyxy[0][3][3].item())), (255,255,255))
cv2.rectangle(tmp_img, (int(results.xyxy[0][4][0].item()), int(results.xyxy[0][4][1].item())), (int(results.xyxy[0][4][2].item()), int(results.xyxy[0][4][3].item())), (255,255,255))
cv2.imwrite('result1.png', tmp_img)

 

 

 

result1.png

 

people1.png
people2.png
people3.png
people4.png
people5.png

 

 

이미지 자르기, 사각형 박스 씌우기는 yolo프로그램과는 상관없이 좌표를 np 어레이로 변형시켜서 안에서 수작업으로 조절하는 형식인것 같은데 yolo에서 적용할 수 있는 명령어같은게 없나? 싶긴 하다.. 아직 그렇게 깊게 파고들 레벨이 아니라서 의구심만 갖고 일단 보류..

 

 

이미지 crop 시 좌표 지정 - [y min : y max, x min : x max]

 

이미지 rectangle시 좌표 지정 - [x min, y min, x max, y max]

 

 

 

 

실전 머신러닝 적용 강의 복습 + 깃허브 탐방

 

colab를 통해 코드를 실행시키는것이 굉장히 실용적인것 같아 더 찾아보았는데, 실제로 구글 드라이브를 마운트시켜서 하드역할을 시켜 데이터를 저장할 수 있었다!

 

https://jimmy-ai.tistory.com/14

 

코랩 구글 드라이브 연동 (Colab에서 마운트 하는 방법 + 디렉토리 확인)

이번 포스팅에서는 구글 Colab에서 드라이브 내 파일을 불러오고 파이썬에서 작업한 결과 파일을 드라이브에 저장 가능하도록 Colab과 드라이브를 연동하는 방법에 대해서 다루어보도록 하겠습니

jimmy-ai.tistory.com

 

이거 진짜 colab 환경 내에서 다 할 수 있는거 아닌가 싶을정도인데..?

 

 

 

 

 

 

 

 

 

P.S.

으악 악몽의 오류

아직도 이 텐서플로우 관련 문제를 풀지 못하고있다.. CUDA11 설치하는데 뭐 이렇게 트러블이 많이 나는지

 

1) CUDA11 설치실패

 

 구글링을 해보니 비슷한 사례가 꽤 많이 일어난 것 같다. Visual Studio 관련 설치중에 실패가 일어난다는 얘기가 많아 Visual Studio Integration를 제외하고 설치해서 해결..을 하긴 했는데..

 

2) 설치후 다시 코드를 실행시켜도 같은 에러가 발생

 

 도대체 뭐가 문제일까.. 애초에 Visual Studio에서 텐서플로우를 돌리는데 필요한 파일이라 VS관련 설치파일 안에 들어있는 파일일지도 모르겠다