forked from ohyicong/Google-Image-Scraper
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ImageProcessor.py
100 lines (79 loc) · 3.23 KB
/
ImageProcessor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#import helper libraries
import time
import urllib.request
from urllib.parse import urlparse
import os
import sys
import requests
import io
from PIL import Image
import cv2
import numpy as np
#custom patch libraries
import patch
class ImageProcessor():
def __init__(self, output_size):
self.output_size = output_size
def detect_faces(self, image):
# Convert the image from PIL.Image format to a NumPy array
image = np.array(image)
# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Load the face detector
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Detect faces in the image
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
# Return the list of face bounding boxes
return faces
def process_image(self, image):
width, height = image.size
if width < self.output_size or height < self.output_size:
raise Exception("Has smaller resolution than output_size, skipping")
if width < height:
new_width = self.output_size
new_height = int(height * self.output_size / width)
else:
new_width = int(width * self.output_size / height)
new_height = self.output_size
image = image.resize((new_width, new_height), Image.ANTIALIAS)
face_boxes = self.detect_faces(image)
images = []
if len(face_boxes) == 0:
print("[INFO] No faces found...")
for face_box in face_boxes:
center_x = face_box[0] + face_box[2] / 2
center_y = face_box[1] + face_box[3] / 2
top = center_y - self.output_size / 2
left = center_x - self.output_size / 2
bottom = center_y + self.output_size / 2
right = center_x + self.output_size / 2
# Adjust top and left values to ensure they do not go outside the bounds of the original image
if top < 0:
bottom = bottom + abs(top)
top = 0
if left < 0:
right = right + abs(left)
left = 0
# Adjust bottom and right values to ensure they do not go outside the bounds of the original image
if bottom > new_height:
rest = bottom - new_height
top = top - rest
bottom = new_height
if right > new_width:
rest = right - new_width
left = left - rest
right = new_width
new_image = image.copy()
new_image = new_image.crop((left, top, right, bottom))
images.append(new_image)
return images
def process_url(self, image_url):
image = requests.get(image_url,timeout=5)
if image.status_code != 200:
raise Exception("Discarded due to error code %s"%(image.status_code))
image_from_web = Image.open(io.BytesIO(image.content))
return image_from_web
# except OSError:
# print("[WARNING] OS Error: %s, trying anyway", %(e))
# rgb_im = image_from_web.convert('RGB')
# process_image(rgb_im, output_size, image_path)