1 year ago

#349785

test-img

Temitope Ayoade

cropping image using dlib and open cv with python

enter image description here

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

python

opencv

deep-learning

computer-vision

dlib

0 Answers

Your Answer

Accepted video resources