Skip to content

cloud17shield/polished_flask_stream

Repository files navigation

FYP

Real-time Drivers’ Behavior Anomaly Detection

Our project website https://i.cs.hku.hk/~msp18018/


Code

Client (Camera)

Python environment: 3.6

Packages need to be installed on local machine:

pip install opencv-python

client.py socket client, using in local client, using web camera to socket transmit the live video data(frames). Run it in you local machine(e.g. laptop). Camera device should be included in the local machine.

WEB

consumer_4together_polished.py python flask WEB UI, front end, render 4 detection output and keep the socket server connection online. Run following command to start the web server:

python3 consumer_4together_polished.py

Spark Streaming

d…*_streaming.py first version code, using collect() methods to computing in driver, not scaleable.

d…*_streaming_distributed.py second version code, using map() method to parallel compute, using broadcast() to share model and static variable in each node. Need warm up due to the broadcast, waiting for several minutes it will stable, (depends on the model size). Also with lower latency and higher FPS. Run following(e.g. drowsy):

/opt/spark-2.4.3-bin-hadoop2.7/bin/spark-submit --master yarn --deploy-mode cluster --num-executors 5 --packages org.apache.spark:spark-streaming-kafka-0-8_2.11:2.4.3 ~/DrunkDetection/drowsy_streaming_distributed.py

Models has already downloaded in our K8s containers and the image has been backed. There is no need to download models. But if there are any problems relating to model missing (e.g. 'Unable to open file...'), models can be download by:

Model Download

Install gdown in linux server to download model from my google drive (these model is larger >100M):

pip3 install --user gdown

Then download the model using gdown:

# Distracted_mobilenet_full.h5
gdown "https://drive.google.com/uc?id=1Na8QXTkO0J1OpMRs0u-AQ9A6jUvLo7DL"

# Distracted_mobilenet_full_6c.h5
gdown "https://drive.google.com/uc?id=1WlraW_Wvb-nIUk_AeXDGGGkNv_mjP1tg"

# Drunk and drowsy human face landmarking
# shape_predictor_68_face_landmarks.dat
gdown "https://drive.google.com/uc?id=1s4Gzq8H_XNqgVqIyLZhOSbNVx-fbKrBg"
# the code have already contains pickle file and csv (small)

For Imageai object detection pre-trained model:

# yolov3
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/yolo.h5

# yolov3tiny
wget https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/yolo-tiny.h5

Spark, Kafka... parameters

All the environment settings are included in the newest container images.

To look into detail in our parameter settings, you can refer to https://github.com/cloud17shield/k8s_env: spark-default.conf, and refer to k8s.sh, some parameters and steps there.

Set spark configuration, download all the model and copy to every node, modify code: model path, ip and port, Kafka related.

Then run with spark streaming submit cluster mode. Run the flask server and fresh, run client start the camera. You will see the result in the browser.