Skip to content
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
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions kubeedge-emotion-recognition-demo/README.md
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.
Copy link
Member

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?


```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.
9 changes: 9 additions & 0 deletions kubeedge-emotion-recognition-demo/client/Dockerfile
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" ]
72 changes: 72 additions & 0 deletions kubeedge-emotion-recognition-demo/client/emotion.py
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()
Loading