-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
24bfc92
commit 230dbae
Showing
57 changed files
with
33,416 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
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.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
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
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
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
File renamed without changes
File renamed without changes
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
230dbae
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
capitalens – ./
capitalens-yutakobayashi.vercel.app
capitalens.vercel.app
parliament-data.vercel.app
capitalens-git-main-yutakobayashi.vercel.app