作者cosmicray (宇宙射线)
看板Python
标题[问题] 新手询问关於裁切图片并储存
时间Mon Aug 23 15:33:13 2021
各位大神好
想请问假设我希望将它们人脸画矩形裁切出来
并个别储存成档案
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://webptt.com/cn.aspx?n=bbs/Python/M.1629703995.A.69B.html
1F:推 lycantrope: dict长怎麽样? 是{"img_name": [bbox_1,...,bbox_n]}? 08/23 16:33
2F:推 lycantrope: plt.imshow与plt.show要放到bbox for loop结束後才跑 08/23 16:51
乾我竟然忘记缩排的事...把img到show移出来有解决了
dict的话我倒是不清楚诶,只知道是python dictionary
总之太感谢您了!
※ 编辑: cosmicray (140.117.160.78 台湾), 08/23/2021 17:16:03