Update crop_face.py
authorakrynski <akrynski@wp.pl>
Sun, 6 Apr 2014 16:48:38 +0000 (18:48 +0200)
committerakrynski <akrynski@wp.pl>
Sun, 6 Apr 2014 16:48:38 +0000 (18:48 +0200)
I propose some changes, to automate file cropping

modules/contrib/doc/facerec/src/crop_face.py

index 6d6669d..11472a9 100755 (executable)
@@ -81,9 +81,32 @@ def CropFace(image, eye_left=(0,0), eye_right=(0,0), offset_pct=(0.2,0.2), dest_
   image = image.resize(dest_sz, Image.ANTIALIAS)
   return image
 
+def readFileNames():
+    try:
+        inFile = open('path_to_created_csv_file.csv')
+    except:
+        raise IOError('There is no file named path_to_created_csv_file.csv in current directory.')
+        return False
+
+    picPath = []
+    picIndex = []
+
+    for line in inFile.readlines():
+        if line != '':
+            fields = line.rstrip().split(';')
+            picPath.append(fields[0])
+            picIndex.append(int(fields[1]))
+
+    return (picPath, picIndex)
+
+
 if __name__ == "__main__":
-  image =  Image.open("arnie.jpg")
-  CropFace(image, eye_left=(252,364), eye_right=(420,366), offset_pct=(0.1,0.1), dest_sz=(200,200)).save("arnie_10_10_200_200.jpg")
-  CropFace(image, eye_left=(252,364), eye_right=(420,366), offset_pct=(0.2,0.2), dest_sz=(200,200)).save("arnie_20_20_200_200.jpg")
-  CropFace(image, eye_left=(252,364), eye_right=(420,366), offset_pct=(0.3,0.3), dest_sz=(200,200)).save("arnie_30_30_200_200.jpg")
-  CropFace(image, eye_left=(252,364), eye_right=(420,366), offset_pct=(0.2,0.2)).save("arnie_20_20_70_70.jpg")
+  [images, indexes]=readFileNames()
+if not os.path.exists("modified"):
+    os.makedirs("modified")
+for img in images:
+    image =  Image.open(img)
+    CropFace(image, eye_left=(252,364), eye_right=(420,366), offset_pct=(0.1,0.1), dest_sz=(200,200)).save("modified/"+img.rstrip().split('/')[1]+"_10_10_200_200.jpg")
+    CropFace(image, eye_left=(252,364), eye_right=(420,366), offset_pct=(0.2,0.2), dest_sz=(200,200)).save("modified/"+img.rstrip().split('/')[1]+"_20_20_200_200.jpg")
+    CropFace(image, eye_left=(252,364), eye_right=(420,366), offset_pct=(0.3,0.3), dest_sz=(200,200)).save("modified/"+img.rstrip().split('/')[1]+"_30_30_200_200.jpg")
+    CropFace(image, eye_left=(252,364), eye_right=(420,366), offset_pct=(0.2,0.2)).save("modified/"+img.rstrip().split('/')[1]+"_20_20_70_70.jpg")