Skip to content

Commit

Permalink
move face model
Browse files Browse the repository at this point in the history
  • Loading branch information
yutakobayashidev committed Oct 26, 2023
1 parent 24bfc92 commit 230dbae
Show file tree
Hide file tree
Showing 57 changed files with 33,416 additions and 208 deletions.
58 changes: 58 additions & 0 deletions backend/face_model/download_model_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import os
import requests
from bs4 import BeautifulSoup
import cv2
import numpy as np


def download_images(search_query, num_images, output_dir):
url = f"https://www.bing.com/images/search?q={search_query}&form=HDRSC2"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134"
}

response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, "html.parser")
image_tags = soup.find_all("img", {"class": "mimg"})

if not os.path.exists(output_dir):
os.makedirs(output_dir)

i = 0
face_cascade = cv2.CascadeClassifier(
cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
)

for img_tag in image_tags:
img_url = img_tag.get("src") or img_tag.get("data-src")

# Ignore data URL
if img_url.startswith("data:image"):
continue

img_data = requests.get(img_url).content
img = np.asarray(bytearray(img_data), dtype="uint8")
img = cv2.imdecode(img, cv2.IMREAD_COLOR)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(
gray, scaleFactor=1.05, minNeighbors=5, minSize=(40, 40)
)

if len(faces) == 1:
with open(os.path.join(output_dir, f"{search_query}_{i}.jpg"), "wb") as f:
f.write(img_data)
print(f"Downloaded {i + 1}/{num_images} images")

i += 1
if i >= num_images:
break


if __name__ == "__main__":
politician_names = ["伊藤博文"]
num_images = 10
base_output_dir = "./politicians"

for name in politician_names:
output_dir = os.path.join(base_output_dir, name)
download_images(name, num_images, output_dir)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 1 addition & 2 deletions backend/package.json → backend/face_model/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "politicians",
"name": "face_model",
"version": "1.0.0",
"description": "顔認識用の画像データ",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
File renamed without changes.
43 changes: 43 additions & 0 deletions backend/lib/face/face_icon_extractor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import cv2
import urllib.request
import numpy as np


def url_to_image(url):
with urllib.request.urlopen(url) as resp:
image = np.asarray(bytearray(resp.read()), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
return image


def extract_face_icon_from_url(image_url, casc_path="haarcascade_frontalface_default.xml", padding_ratio=0.7):
face_cascade = cv2.CascadeClassifier(casc_path)

image = url_to_image(image_url)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

faces = face_cascade.detectMultiScale(
gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))

if len(faces) == 0:
return None

x, y, w, h = faces[0]
center_x = x + w // 2
center_y = y + h // 2

square_length = min(w, h) * (1 + padding_ratio)

start_x = int(center_x - square_length // 2)
start_y = int(center_y - square_length // 2)
end_x = int(start_x + square_length)
end_y = int(start_y + square_length)

start_x = max(0, start_x)
start_y = max(0, start_y)
end_x = min(image.shape[1], end_x)
end_y = min(image.shape[0], end_y)

icon = image[start_y:end_y, start_x:end_x]

return icon
Loading

1 comment on commit 230dbae

@vercel
Copy link

@vercel vercel bot commented on 230dbae Oct 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.