### Instruction:
Develop a Python script that analyzes a directory of images, detects faces using OpenCV, and stores metadata (e.g., number of faces, average face size) in an SQLite database. The script should also provide a query interface for retrieving stored metadata.

### Response:

To create a Python script that analyzes a directory of images, detects faces using OpenCV, and stores metadata in an SQLite database, follow these steps:

1. Install the required libraries:
```bash
pip install opencv-python opencv-python-headless numpy sqlite3
```

2. Create a new Python file (e.g., `face_detection.py`) and import the necessary modules:

```python
import cv2
import os
import sys
import sqlite3
import numpy as np
from pathlib import Path
```

3. Define a function to initialize the SQLite database:

```python
def init_database():
    conn = sqlite3.connect("image_metadata.db")
    cursor = conn.cursor()
    cursor.execute(
        """
        CREATE TABLE IF NOT EXISTS image_metadata (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            filename TEXT NOT NULL,
            num_faces INTEGER,
            avg_face_size REAL
        )
        """
    )
    conn.commit()
    return conn
```

4. Define a function to detect faces in an image:

```python
def detect_faces(image_path):
    face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")

    img = cv2.imread(image_path)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

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

    return faces
```

5. Define a function to analyze a directory of images and store metadata in the SQLite database:

```python
def analyze_images(directory, conn):
    cursor = conn.cursor()

    for entry in os.scandir(directory):
        if entry.is_file() and entry.name.lower().endswith((".png", ".jpg", ".jpeg")):
            print(f"Analyzing {entry.name}...")

            faces = detect_faces(entry.path)
            num_faces = len(faces)

            if num_faces > 0:
                avg_face_size = np.mean([w * h for x, y, w, h in faces])
            else:
                avg_face_size = None

            cursor.execute(
                "INSERT INTO image_metadata (filename, num_faces, avg_face_size) VALUES (?, ?, ?)",
                (entry.name, num_faces, avg_face_size),
            )

    conn.commit()
```

6. Define a function to query the stored metadata:

```python
def query_metadata(conn):
    cursor = conn.cursor()

    print("\nStored metadata:")
    for row in cursor.execute("SELECT filename, num_faces, avg_face_size FROM image_metadata"):
        print(f"Filename: {row[0]}, Number of Faces: {row[1]}, Average Face Size: {row[2]}")
```

7. Add the main script logic:

```python
if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Usage: python face_detection.py <image_directory>")
        sys.exit(1)

    image_directory = Path(sys.argv[1])

    if not image_directory.is_dir():
        print("Invalid directory path.")
        sys.exit(1)

    conn = init_database()
    analyze_images(image_directory, conn)
    query_metadata(conn)
    conn.close()
```

Now you can run the script with the following command:

```bash
python face_detection.py /path/to/image/directory
```

This will analyze all images in the specified directory, store metadata in an SQLite database (`image_metadata.db`), and display the stored metadata.