π μ½λ μμλ λ§μ§λ§ λ무 μ΄ν΄κ° μλΌμ νλνλ λ―μ΄λ³΄μλ€.
import torch import cv2 model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True) # model λ³μμ torchλ₯Ό νμ©νμ¬ ultralyticsμ yolov5 λ ν¬μ§ν 리μ yolov5s λͺ¨λΈμ κ°μ Έμ΄ img = cv2.imread('zidane.jpg') # μ§λ¨ μ΄λ―Έμ§λ₯Ό opencv2λ₯Ό μ΄μ©νμ¬ μ½μ΄μ img λ³μμ μ μ₯ results = model(img) # results μ yolov5 λͺ¨λΈμ μ΄μ©νμ¬ μ§λ¨ μ΄λ―Έμ§ μΈμ results.save() # μ»΄ν¨ν° λμ€ν¬μ μ μ₯ result = results.pandas().xyxy[0].to_numpy() # resultsλ₯Ό pandas xmin xmax ymin ymax μ’νλ‘ λ°κΎΈμ΄ μ€ ν # to_numpy() ν¨μλ‘ ndarray νμμΌλ‘ λ°κΎΈμ΄ μ€ result = [item for item in result if item[6]=='person'] # κ°μ²΄κ° μ¬λμΈ κ²λ§ λ½μμ΄ tmp_img = cv2.imread('zidane.jpg') print(tmp_img.shape) # shape λ μ΄λ―Έμ§λ₯Ό ν μ΄ μμΌλ‘ μλΌμ£Όλ κ² cropped = tmp_img[int(result[0][1]):int(result[0][3]), int(result[0][0]):int(result[0][2])] # μ¬λ κ°μ²΄ -> result[0] μ λ΄κ²¨μκ³ [1] [3] [0] [2] x y x y μ’νλ‘ # μ¬μ§μ μλΌμ€ print(cropped.shape) cv2.imwrite('zidane.png', cropped) 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())), (0,0,255)) # tmg_imgμ μ μ’νλ‘ λ€λͺ¨ νμλ₯Ό ν΄ μ€λ€. cv2.imwrite('zidane2.png', tmp_img) # μ΄λ―Έμ§ zidane.png λΌλ μ΄λ¦μΌλ‘ μ μ₯
zidane.jpg
results -> λ³νλ μ΄λ―Έμ§
zidane.png -> 첫 λ²μ§Έ κ°μ²΄λ₯Ό μλ₯Έ μ¬μ§
zidane.png -> 첫 λ²μ§Έ κ°μ²΄μ rectangle ν¨μλ‘ κ·Έλ¦Όμ κ·Έλ €μ€ λͺ¨μ΄
π μ²μμ λ무 μ΄λ €μ μ΄λ―Έμ§ νμ΅μ λ΄κ° μν¬ μ μμκΉ λΌλ μκ°μ΄ λ€μμ§λ§ μμ κ³μ ν΄λ³΄λ μλλ건 μλ€.
ν μ€ ν μ€ λ€ λ―μ΄λ³΄λ©΄μ μΆμμ μΌλ‘λΌλ μ΄ν΄νλ©΄μ λμ΄κ°λ μ 체μ μΌλ‘ μ΄ν΄κ° λκΈ° μμνλ€.
5μΌλμ νλ νλ‘μ νΈμ μ£Όμ κΈ°λ₯μ΄ μ΄λ―Έμ§μ 물체λ₯Ό μΈμνμ¬ μΉ΄ν κ³ λ¦¬μ μΆκ°ν΄ μ£Όλ κ²μ΄λ€.
λ§μ μ§νν΄ λ³΄λ νμ΅λ λͺ¨λΈμ κ°μ Έμ μ¬μ©νλ κ²μ μ¬μ κ³ μ½λλ₯Ό νμ€νμ€ μ΄ν΄νλ€ λ³΄λ λ΄κ° μνλ μμΌλ‘ λ°κΎΈμ΄ μ¬μ©ν μ μκ² λμλ€.
λ΄μΌμ yoloλ‘ μ΄λ―Έμ§ νμ΅λ μμΌλ³΄κ³ λ€λ₯Έ νμ΅λ λͺ¨λΈλ μ¬μ©ν΄ 보μμΌκ² λ€.
μκ³ λ¦¬μ¦λ νμ΄μΌνλλ° μ€λ λͺ»νμλ€ γ γ λ΄μΌ λ λ¬Έμ νμ΄μΌμ§ γ γ