Package: auto-crop
auto-cropImage auto crop toolbox. Supports image cropping with any aspect ratio, based onface-detectionandGAIC: Grid-Anchor-based-Image-Cropping-Pytorch.This project only supports python3 and pytorch > 1.0ContentsSetupDemoDifferent from GAICReferenceCitationSetupYou can choose to install via pypi or install it via source code.From Pypiauto-cropcan be installed directly through pypipipinstallauto-cropFrom Source Code# colone repositorygitclonehttps://github.com/lih627/autocrop.git# install autocropcd/path/to/autocrop
pythonsetup.pyinstallNote: If there is errors when compiling CPP and CUDA extensions, you can choose to compile CPP/CUDA api separately.cdautocrop/model/rod_align
pythonsetup.pyinstallcd../roi_aligm
pythonsetup.pyinstallDemoHere is a simple demo. Fromdemo.pyFirst, build a cropper. Cropper containsGAIC pretrained models, and you can select load aDSFD Face-Detecoror not.fromautocropimportcropperautocropper=cropper.AutoCropper(model='mobilenetv2',# 'mobilenetv2' or 'shufflenetv2'cuda=True,# if GUDA is avaliable and True, Inference on GPUuse_face_detector=True)# Use Face Detector to filter RoIsThen, use cropper to crop RGB formate image. The selectable parameters are the number of cropping results, the aspect ration and whether to used face detection results to assist in generating RoIs.crop_retis a list with sizetopK x 4,  Each cropping result is encoded as[xmin, ymin, xmax, ymax]in pixel coordinate system.importcv2# BGR to RGBimg=cv2.imread('imgs/demo.jpg')img_=img[:,:,(2,1,0)]# get crop resultcrop_ret=autocropper.crop(img_,topK=1,crop_height=1,crop_width=1,filter_face=True,# True: Crop result will not contain half facesingle_face_center=True)# True: face in the crop result's width centerYou can visualize the cropping resultsforbboxincrop_ret:r,g,b=int(random.random()*255),int(random.random()*255),int(random.random()*255)cv2.rectangle(img,(bbox[0],bbox[1]),(bbox[2],bbox[3]),(b,g,r))cv2.imshow('ret',img)cv2.waitKey()Different from GAICThis project is mostly based on GAIC, and the modules are listed as follows:It is slightly different from GAIC in practice, as shown below:We can specify any crop ratioGAIC supports RoIs with uncertain aspect ratios and several RoIs with fixed aspect ratios(1:1, 4:3, 16:9). In practical applications, image cropping needs to select the cropping area according to the fixed aspect ratio. I modified the code of the bboxes generation part. For RoIs evaluation, I used the GAIC pre-trained model.If there is only half a face in the bounding box, filter out the bounding boxAt the same time, in practical applications, when the distribution of people in the picture is not fixed, for example, when two people stand on the left and right sides of the picture, the RoI selected by GAIC may tear the human body. We adopt the face detection method to filter out some non-conformities. The required RoI will be evaluated after.There is a comparison:When the bounding box has only one face, the face should be in the middle of the box as much as possibleWe have added additional options when generating anchor boxes. If there is only one face in a RoI, use the RoI with the face in the middle of the RoI's width direction. seeautocrop/cropper.pyfor details.There is a comparison:ReferenceGAIC: Grid-Anchor-based-Image-Cropping-PytorchMIT LicenseDSFD-Pytorch-InferenceApache-2.0 LicenseCitationIf you find this code useful, remember to cite the original authors:For GAIC:@inproceedings{zhang2019deep,
  title={Reliable and Efficient Image Cropping: A Grid Anchor based Approach},
  author={Zeng, Hui, Li, Lida， Cao, Zisheng and Zhang, Lei},
  booktitle={IEEE Conference on Computer Vision and Pattern Recognition},
  year={2019}
}For DSFD:@inproceedings{li2018dsfd,
  title={DSFD: Dual Shot Face Detector},
  author={Li, Jian and Wang, Yabiao and Wang, Changan and Tai, Ying and Qian, Jianjun and Yang, Jian and Wang, Chengjie and Li, Jilin and Huang, Feiyue},
  booktitle={Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition},
  year={2019}
}