Skip to content

Commit ca62a22

Browse files
committed
Merge branch 'main' of https://github.com/cs-joy/Android
2 parents 5e82f6b + 61164e1 commit ca62a22

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

FaceDetection/docs/documentation.md

+31
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,34 @@ Open `activity.xml` file, located at `src > main > res > layout > activity.xml`.
133133
134134
</RelativeLayout>
135135
```
136+
### Write Python Script
137+
go to `src/main/python` then create a new file called `face_detection.py` (or whatever you would like to give as an file name)
138+
after that open the new file (`face_detection.py`)
139+
140+
copy the following code and paste into your python file
141+
```py
142+
import numpy as np
143+
import cv2
144+
from PIL import Image
145+
import base64
146+
import io
147+
import face_recognition
148+
149+
def main(data):
150+
decoded_data = base64.b64decode(data)
151+
np_data = np.fromstring(decoded_data, np.uint8)
152+
img = cv2.imdecode(np_data, cv2.IMREAD_UNCHANGED)
153+
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
154+
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
155+
156+
face_locations = face_recognition.face_locations(img_gray)
157+
for (top, right, bottom, left) in face_locations:
158+
cv2.rectangle(img_rgb, (left, top), (right, bottom), (0, 0, 255), 8)
159+
160+
pil_img = Image.fromarray(img_rgb)
161+
buff = io.BytesIO();
162+
pil_img.save(buff, format="PNG")
163+
img_str = base64.b64encode(buff.getvalue())
164+
165+
return "" + str(img_str, 'utf-8')
166+
```

0 commit comments

Comments
 (0)