使用DLIB裁剪图像,并用Python打开简历
我想裁剪每个学生的护照,专辑中的学生编号的基础,我写了一本可以随机裁剪护照,但我希望第一张护照为001.jpg,而第二次护照则是002.JPG的,但它可以随机裁剪护照。我的代码如下
import cv2
import dlib
detector = dlib.get_frontal_face_detector()
new_path ='path to save the cropped passports'
def MyRec(rgb,x,y,w,h,v=20,color=(200,0,0),thikness =2):
cv2.line(rgb, (x,y),(x+v,y), color, thikness)
cv2.line(rgb, (x,y),(x,y+v), color, thikness)
cv2.line(rgb, (x+w,y),(x+w-v,y), color, thikness)
cv2.line(rgb, (x+w,y),(x+w,y+v), color, thikness)
cv2.line(rgb, (x,y+h),(x,y+h-v), color, thikness)
cv2.line(rgb, (x,y+h),(x+v,y+h), color, thikness)
cv2.line(rgb, (x+w,y+h),(x+w,y+h-v), color, thikness)
cv2.line(rgb, (x+w,y+h),(x+w-v,y+h), color, thikness)
def save(img,name, bbox, width=180,height=227):
x, y, w, h = bbox
imgCrop = img[y:h, x: w]
imgCrop = cv2.resize(imgCrop, (width, height))#we need this line to reshape the images
cv2.imwrite(name+".jpg", imgCrop)
def faces():
frame =cv2.imread('faces3.jpg')
gray =cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces = detector(gray)
fit =20
for counter,face in enumerate(faces):
print(counter)
x1, y1 = face.left(), face.top()
x2, y2 = face.right(), face.bottom()
cv2.rectangle(frame,(x1,y1),(x2,y2),(220,255,220),1)
MyRec(frame, x1, y1, x2 - x1, y2 - y1, 10, (0,250,0), 3)
save(gray,new_path+str(counter),(x1-fit,y1-fit,x2+fit,y2+fit))
图所示
I want to crop the passport of each student, base on the student number in the album, i wrote an algorithm which can crop the passports but it crop the passport randomly, whereas i want the first passport to be 001.jpg while the second passport to 002.jpg . my code is included below
import cv2
import dlib
detector = dlib.get_frontal_face_detector()
new_path ='path to save the cropped passports'
def MyRec(rgb,x,y,w,h,v=20,color=(200,0,0),thikness =2):
cv2.line(rgb, (x,y),(x+v,y), color, thikness)
cv2.line(rgb, (x,y),(x,y+v), color, thikness)
cv2.line(rgb, (x+w,y),(x+w-v,y), color, thikness)
cv2.line(rgb, (x+w,y),(x+w,y+v), color, thikness)
cv2.line(rgb, (x,y+h),(x,y+h-v), color, thikness)
cv2.line(rgb, (x,y+h),(x+v,y+h), color, thikness)
cv2.line(rgb, (x+w,y+h),(x+w,y+h-v), color, thikness)
cv2.line(rgb, (x+w,y+h),(x+w-v,y+h), color, thikness)
def save(img,name, bbox, width=180,height=227):
x, y, w, h = bbox
imgCrop = img[y:h, x: w]
imgCrop = cv2.resize(imgCrop, (width, height))#we need this line to reshape the images
cv2.imwrite(name+".jpg", imgCrop)
def faces():
frame =cv2.imread('faces3.jpg')
gray =cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces = detector(gray)
fit =20
for counter,face in enumerate(faces):
print(counter)
x1, y1 = face.left(), face.top()
x2, y2 = face.right(), face.bottom()
cv2.rectangle(frame,(x1,y1),(x2,y2),(220,255,220),1)
MyRec(frame, x1, y1, x2 - x1, y2 - y1, 10, (0,250,0), 3)
save(gray,new_path+str(counter),(x1-fit,y1-fit,x2+fit,y2+fit))
Image
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论