[問題] 新手詢問關於裁切圖片並儲存
各位大神好
想請問假設我希望將它們人臉畫矩形裁切出來
並個別儲存成檔案
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
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.117.160.78 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1629703995.A.69B.html
推
08/23 16:33,
2年前
, 1F
08/23 16:33, 1F
推
08/23 16:51,
2年前
, 2F
08/23 16:51, 2F
推
08/23 17:08,
2年前
, 3F
08/23 17:08, 3F
乾我竟然忘記縮排的事...把img到show移出來有解決了
dict的話我倒是不清楚誒,只知道是python dictionary
總之太感謝您了!
※ 編輯: cosmicray (140.117.160.78 臺灣), 08/23/2021 17:16:03