-
Notifications
You must be signed in to change notification settings - Fork 172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add emotion-recognition-demo #50
Open
kuramal
wants to merge
2
commits into
kubeedge:master
Choose a base branch
from
kuramal:faceemotion
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,66 @@ | ||
# KubeEdge Emotion Recognition Demo | ||
|
||
## Description | ||
|
||
KubeEdge Emotion Recognition is demo of emotion recognition with raspberry Pi. Raspberry Pi is equipped with an RS/E4 standard camera. | ||
|
||
<img src="images/raspberry-video.jpg"> | ||
|
||
## Deployment Diagram | ||
|
||
<img src="images/emotion-recognition-arch.jpg"> | ||
|
||
## Prerequisites | ||
|
||
### Hardware Prerequisites | ||
|
||
1. RaspBerry-Pi (RaspBerry-Pi 4 has been used for this demo) | ||
2. RS/E4 Camera | ||
3. Display | ||
|
||
## Steps to reproduce | ||
|
||
1. Clone the kubeedge/examples repository. | ||
|
||
```console | ||
git clone https://github.com/kubeedge/examples.git /root/examples | ||
``` | ||
|
||
2. Deploy Emotion Recognition Server. | ||
|
||
```console | ||
kubectl apply -f /root/examples/kubeedge-emotion-recognition-demo/emotion-server.yaml | ||
``` | ||
|
||
3. Deploy Emotion Recognition Client To raspberry Pi. | ||
|
||
```console | ||
kubectl apply -f /root/examples/kubeedge-emotion-recognition-demo/emotion-client.yaml | ||
``` | ||
``` | ||
spec: | ||
containers: | ||
- env: | ||
- name: DISPLAY | ||
value: :0 | ||
- name: FACEEMOTION_SERVER | ||
value: $EMOTION_SERVER #set emotion server address | ||
- name: FACEEMOTION_PORT | ||
value: $EMOTION_CLIENT #set emotion server port | ||
volumeMounts: | ||
- mountPath: /tmp/.X11-unix | ||
name: x11 | ||
- mountPath: /dev/video0 | ||
name: video | ||
securityContext: | ||
privileged: true | ||
volumes: | ||
- name: x11 | ||
hostPath: | ||
path: /tmp/.X11-unix #match your display device | ||
- name: video | ||
hostPath: | ||
path: /dev/video0 #match your camera device | ||
``` | ||
|
||
**Note:** Only when a face is detected , the expression will appear on the display. |
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,9 @@ | ||
From kuramal/onnxruntime_opencv:arm32 | ||
|
||
RUN pip install requests | ||
|
||
ADD ./emotion.py /home | ||
|
||
ADD ./haarcascade_frontalface_default.xml /home | ||
|
||
ENTRYPOINT [ "python", "/home/emotion.py" ] |
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,72 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Created on Tue Jan 7 03:18:26 2020 | ||
|
||
@author: gaowei | ||
""" | ||
|
||
import os | ||
import base64 | ||
import json | ||
import requests | ||
import cv2 | ||
import numpy as np | ||
|
||
faceCascade = cv2.CascadeClassifier('/home/haarcascade_frontalface_default.xml') | ||
|
||
env_dist = os.environ | ||
|
||
faceemotion_server = env_dist.get('FACEEMOTION_SERVER') | ||
faceemotion_port = env_dist.get('FACEEMOTION_PORT') | ||
|
||
cap = cv2.VideoCapture(0) | ||
timeF = 10 | ||
count = 0 | ||
request_url = "http://%s:%s/model/methods/predict" % (faceemotion_server, faceemotion_port) | ||
init = 0 | ||
|
||
while(True): | ||
count = count + 1 | ||
headers = {'accept': 'application/json','content-type': 'application/json'} | ||
ret, frame = cap.read() | ||
if (count%timeF != 0): | ||
continue | ||
|
||
img = cv2.flip(frame, 1) | ||
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | ||
faces = faceCascade.detectMultiScale( | ||
gray, | ||
scaleFactor=1.2, | ||
minNeighbors=5, | ||
minSize=(20, 20) | ||
) | ||
|
||
if len(faces) == 0 and init == 1: | ||
continue | ||
|
||
init = 1 | ||
|
||
small_frame = cv2.resize(frame,(0,0),fx = 0.88,fy = 0.88) | ||
image = cv2.imencode('.jpg',small_frame)[1] | ||
img_BASE64 = str(base64.b64encode(image))[2:-1] | ||
|
||
post_data = {"img_base64": img_BASE64} | ||
data = json.dumps(post_data).encode(encoding = 'utf-8') | ||
response = requests.post(url=request_url, data=data, headers=headers) | ||
res = json.loads(response.text)#dict | ||
res_dict = json.loads(res["value"]) | ||
src = res_dict["img_url"] | ||
data_processd = src.split(',')[1] | ||
# base64解码 | ||
image_data = base64.b64decode(data_processd) | ||
# 转换为np数组 | ||
img_array = np.fromstring(image_data, np.uint8) | ||
# 转换成opencv可用格式 | ||
img = cv2.imdecode(img_array, cv2.COLOR_RGB2BGR) | ||
cv2.imshow('img', img) | ||
# Display the resulting frame | ||
if cv2.waitKey(1) & 0xFF == ord('q'): | ||
break | ||
cap.release() | ||
cv2.destroyAllWindows() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Where should the server be deployed? And How do users use the demo?