File size: 3,957 Bytes
8619eb9
 
 
 
 
 
 
 
 
 
289811c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8619eb9
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
---
license: mit
title: FaceDetectionwithGradio
sdk: gradio
emoji: πŸ‘€
colorFrom: indigo
colorTo: yellow
pinned: true
short_description: This project provides a web-based face detection application
---
# Face Detection with Gradio - Phone Camera Access

This project provides a web-based face detection application optimized for accessing your phone's camera.

## Features

- **Phone Camera Access**: Direct camera access through web browser on mobile devices
- **Image Upload**: Alternative option to upload existing images
- **Real-time Face Detection**: Uses OpenCV's Haar Cascade Classifier
- **Mobile-Optimized UI**: Responsive design that works great on phones
- **Easy Sharing**: Can create shareable links to access from any device

## Why Gradio?

Gradio is better for phone camera access because:
- βœ… Works seamlessly with mobile browsers (Chrome, Safari)
- βœ… Automatically requests camera permissions
- βœ… No complex mobile configuration needed
- βœ… Can create public shareable links
- βœ… Responsive design out of the box

## Setup Instructions

### 1. Activate the Virtual Environment

**On Windows:**
```bash
.camera\Scripts\activate
```

**On Mac/Linux:**
```bash
source .camera/bin/activate
```

### 2. Install Dependencies

```bash
pip install -r requirements.txt
```

This will install:
- opencv-python (face detection)
- pillow (image processing)
- numpy (array operations)
- gradio (web UI)

### 3. Run the Application

```bash
python ui.py
```

The app will:
- Start a local web server (default: http://localhost:7860)
- Automatically open in your browser
- Display a URL you can access from your phone

## Accessing from Your Phone

### Option 1: Same WiFi Network (Recommended)

1. Make sure your phone and computer are on the same WiFi network
2. When you run `python ui.py`, look for the output that shows:
   ```
   Running on local URL:  http://127.0.0.1:7860
   Running on network URL: http://192.168.x.x:7860
   ```
3. Open the **network URL** (192.168.x.x:7860) in your phone's browser
4. Grant camera permissions when prompted

### Option 2: Public Link (Easy sharing)

1. Edit [ui.py](ui.py) and change line 170:
   ```python
   share=True,  # Create a public link
   ```
2. Run the app - Gradio will create a temporary public URL
3. Use this URL from any device, anywhere

## Usage

### Phone Camera Tab
1. Click on "πŸ“± Phone Camera" tab
2. Allow camera access when prompted
3. Position your face in the camera view
4. Click "πŸ” Detect Faces" button
5. See the results with detected faces highlighted

### Upload Image Tab
1. Click on "πŸ“€ Upload Image" tab
2. Choose an image from your phone's gallery
3. Click "πŸ” Detect Faces" button
4. View the detection results

## Detection Parameters

The face detection uses these default parameters:
- **scaleFactor**: 1.1 (image pyramid scale)
- **minNeighbors**: 5 (quality threshold)
- **minSize**: (30, 30) pixels (minimum face size)

## Tips for Best Results

- πŸ“Έ Ensure good lighting
- πŸ‘€ Face the camera directly
- πŸ“ Maintain a moderate distance (2-3 feet)
- 🎯 Avoid extreme angles or face coverings
- πŸ’‘ Use modern browsers (Chrome, Safari)

## Troubleshooting

### Camera not working on phone?
- Make sure you granted camera permissions
- Try Chrome or Safari browser
- Check if other apps can access the camera
- Reload the page and try again

### Can't connect from phone?
- Verify both devices are on the same WiFi
- Check firewall settings on your computer
- Try using the public link option (share=True)

### Faces not detected?
- Improve lighting conditions
- Move closer/farther from camera
- Ensure face is clearly visible and frontal
- Remove sunglasses or face coverings

## Files

- [ui.py](ui.py) - Main Gradio application
- [main.py](main.py) - Original Streamlit version
- [requirements.txt](requirements.txt) - Python dependencies

## Deactivating Environment

When done, deactivate the virtual environment:
```bash
deactivate
```