File size: 1,720 Bytes
fa69b72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import time
import requests
from concurrent.futures import ThreadPoolExecutor

# Directory where images will be saved
OUTPUT_DIR = os.path.join("data", "faces")
os.makedirs(OUTPUT_DIR, exist_ok=True)

# URL serves a new face on every GET request
URL = "https://thispersondoesnotexist.com/random-person.jpeg"

# Set a standard User-Agent header to prevent requests from being blocked
HEADERS = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
}

NUM_IMAGES = 700
DELAY_SECONDS = 0.00  # Polite delay between requests per worker
NUM_WORKERS = 6


def download_face(image_index: int) -> None:
    """Worker function to download a single face image."""
    file_path = os.path.join(OUTPUT_DIR, f"face_{image_index:03d}.jpg")

    try:
        response = requests.get(URL, headers=HEADERS, timeout=10)

        if response.status_code == 200:
            with open(file_path, "wb") as f:
                f.write(response.content)
            print(f"[{image_index}/{NUM_IMAGES}] Saved: {file_path}")
        else:
            print(
                f"[{image_index}/{NUM_IMAGES}] Failed with status code: {response.status_code}"
            )

    except Exception as e:
        print(f"[{image_index}/{NUM_IMAGES}] Error: {e}")

    time.sleep(DELAY_SECONDS)


def main():
    print(
        f"Starting download of {NUM_IMAGES} faces using {NUM_WORKERS} workers..."
    )

    with ThreadPoolExecutor(max_workers=NUM_WORKERS) as executor:
        executor.map(download_face, range(1, NUM_IMAGES + 1))

    print("Download complete!")


if __name__ == "__main__":
    main()