[問題] 新手詢問關於裁切圖片並儲存

作者: cosmicray (nothing)   2021-08-23 15:33:13
各位大神好
想請問假設我希望將它們人臉畫矩形裁切出來
並個別儲存成檔案
https://imgur.com/GWpEeXn.jpg
不過我手邊已有annotate_dict.pkl是定界框的資訊
而圖片是存在data_dir資料夾裡
目前卡的點是不會讀取這檔並一次畫出來
當然土法煉鋼一個一個設應該可行但這效率太低QQ
希望有大神願意提點一下感激不盡
import os, sys
import numpy as np
import cv2
import dlib
import matplotlib.pyplot as plt
pkl_path = 'annotation.pkl'
annotate_dict = pickle.load(open(pkl_path,"rb"))
data_dir = 'data_dir'
def draw_bbox():
for img_name in annotate_dict:
img_path= os.path.join(data_dir, img_name)
img = cv2.imread(img_path)
for bbox in annotate_dict[img_name]:
x1, y1, x2, y2 = bbox
cv2.rectangle(img, (x1,y1), (x2,y2), (0,255,0), thickness=2)
img_crop= img[y1:y2, x1:x2]
cv2.imwrite('fuck.jpg',img_crop)
# cv2.rectangle(img, (150,125), (195,190), (0,255,0), thickness=2)
# cv2.rectangle(img, (250,100), (300,160), (0,255,0), thickness=2)
# cv2.rectangle(img, (361,84), (400,145), (0,255,0), thickness=2)
# cv2.rectangle(img, (492,62), (533,118), (0,255,0), thickness=2)
# cv2.rectangle(img, (601,74), (641,128), (0,255,0), thickness=2)
img= img[:,:,::-1] # convert from BGR to RGB
plt.imshow(img)
plt.show()
pass
draw_bbox()
如果這樣執行完只能畫出一個方框而已QQ
https://imgur.com/C4pFIR7.jpg
作者: lycantrope (阿寬)   2021-08-23 16:33:00
dict長怎麼樣? 是{"img_name": [bbox_1,...,bbox_n]}?plt.imshow與plt.show要放到bbox for loop結束後才跑https://reurl.cc/83KqGb

Links booklink

Contact Us: admin [ a t ] ucptt.com