From c110f1f668d46e89e5497c9f497153a652f1d62d Mon Sep 17 00:00:00 2001 From: Patrice Date: Thu, 6 Jun 2019 18:46:25 +0200 Subject: [PATCH] added mock interface --- .vscode/launch.json | 70 ++++++++++++++++++ __pycache__/detector.cpython-35.pyc | Bin 3476 -> 3510 bytes mock/Dockerfile | 14 ++++ mock/app.py | 70 ++++++++++++++++++ mock/cams.json | 20 +++++ mock/clients.json | 20 +++++ mock/requirements.txt | 4 + .gitignore => server/.gitignore | 4 +- .pylintrc => server/.pylintrc | 0 README.md => server/README.md | 0 detector.py => server/detector.py | 16 ++-- main.py => server/main.py | 22 ++++-- model.pb => server/model.pb | Bin .../motion_detector_old.py | 0 {videos => server/videos}/example_01.mp4 | Bin {videos => server/videos}/example_02.mp4 | Bin 16 files changed, 224 insertions(+), 16 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 mock/Dockerfile create mode 100644 mock/app.py create mode 100644 mock/cams.json create mode 100644 mock/clients.json create mode 100644 mock/requirements.txt rename .gitignore => server/.gitignore (84%) rename .pylintrc => server/.pylintrc (100%) rename README.md => server/README.md (100%) rename detector.py => server/detector.py (91%) rename main.py => server/main.py (65%) rename model.pb => server/model.pb (100%) rename motion_detector_old.py => server/motion_detector_old.py (100%) rename {videos => server/videos}/example_01.mp4 (100%) rename {videos => server/videos}/example_02.mp4 (100%) diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..92a2fa2 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,70 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python: Current File (Integrated Terminal)", + "type": "python", + "request": "launch", + "program": "${file}", + "console": "integratedTerminal" + }, + { + "name": "Python: Remote Attach", + "type": "python", + "request": "attach", + "port": 5678, + "host": "localhost", + "pathMappings": [ + { + "localRoot": "${workspaceFolder}", + "remoteRoot": "." + } + ] + }, + { + "name": "Python: Module", + "type": "python", + "request": "launch", + "module": "enter-your-module-name-here", + "console": "integratedTerminal" + }, + { + "name": "Python: Django", + "type": "python", + "request": "launch", + "program": "${workspaceFolder}/manage.py", + "console": "integratedTerminal", + "args": [ + "runserver", + "--noreload", + "--nothreading" + ], + "django": true + }, + { + "name": "Python: Flask", + "type": "python", + "request": "launch", + "module": "flask", + "env": { + "FLASK_APP": "app.py" + }, + "args": [ + "run", + "--no-debugger", + "--no-reload" + ], + "jinja": true + }, + { + "name": "Python: Current File (External Terminal)", + "type": "python", + "request": "launch", + "program": "${file}", + "console": "externalTerminal" + } + ] +} \ No newline at end of file diff --git a/__pycache__/detector.cpython-35.pyc b/__pycache__/detector.cpython-35.pyc index cc09104a042fb3ad9dd786247327723b79c437a6..61c0e5c80031c40386c4ea034c0ddf7c580d8e69 100644 GIT binary patch delta 459 zcmX|7Jxc>Y5PiFMIqx=iQDUJn5b-N|2nmVb0YT9^2!dKDf?iY-@ME(g$XTgqD{G-v z_Et9fC#)?}*{1M6IClYeXCLo9W_D-xG5e7feV!-w^Pk(>-S0vjRT(c-vcW!V++Cpd}6BuZ4$P0vtScg>Z zpkYG(>Q~XfVIUJa2ux%SI3kwl7*=cqNdy+I6JUy%9bM1jCP+E|>g)oW)Z@DER@*-e zXs7g8a%iN*k57CqO8{hvNX?`8Q#NsnP4+a zaFYwJD{gL!Q`QQd<^(}B3Iz>KrHZ3&beM^H0zv#2EN+WeF9(j4I;|9orG;{#G&^4? z&CM5Prix8MqVL?%?v^cUXXmx1YPO8Sn$mCXU}`sd%pw5un9$$uq_s#wF6#%o NUk`d|HlWMi-XFBAS@r+` delta 461 zcmX|5Jxc>Y5PiG3o~KG5CRvq6mtJMGzEhw9$>9XmoQJkQ0j_*veX1 z`U5Of#6MwamsTw71;xtQ6P(%i_RX7}VV_IaCD9oKqW*KY_MUqXk>`#j@oFx-OO`h| zG2lnQo`X0ACC!rPpLjy#BFZ9m&}8<^t>5B|>I~v6ii%^OLw_MoCv{BY9`+sRH*+rv z=N!e@gG!;e#bK56XQUHD1yE@;(_oPg<=XX3(e4!J^|JN6KTrI7v|mE`wr+B;<87=e zU^&cHK0X}@eisF%#5 z?DCV(vP$UaNpmCn%!}-mYqsqWfd%GU4rZoF>KSvDEp!Z#oFddSM3PBYcAAx7;}?UP BS9|~f diff --git a/mock/Dockerfile b/mock/Dockerfile new file mode 100644 index 0000000..e7a2d4b --- /dev/null +++ b/mock/Dockerfile @@ -0,0 +1,14 @@ +FROM python:3.7 + +RUN apt-get update +RUN apt-get install -y gcc libevent-dev python-dev + +COPY ./requirements.txt / +RUN pip install -r /requirements.txt + +COPY ./ /app + +WORKDIR /app +EXPOSE 5000 + +CMD ["gunicorn", "-b", "0.0.0.0:5000", "app"] \ No newline at end of file diff --git a/mock/app.py b/mock/app.py new file mode 100644 index 0000000..5d97945 --- /dev/null +++ b/mock/app.py @@ -0,0 +1,70 @@ +import os +import random +import json +from importlib import import_module +import cv2 +from flask import Flask, jsonify, Response + +application = Flask(__name__) +clients = [] +cams = [] +with open("./clients.json", 'r', encoding='utf-8') as f: + array = f.read() + clients = json.loads(array) + +with open("./cams.json", 'r', encoding='utf-8') as f: + array = f.read() + cams = json.loads(array) + + +class VideoCamera(object): + def __init__(self, url): + self.video = cv2.VideoCapture(url) + + def __del__(self): + self.video.release() + + def get_frame(self): + success, image = self.video.read() + # We are using Motion JPEG, but OpenCV defaults to capture raw images, + # so we must encode it into JPEG in order to correctly display the + # video stream. + ret, jpeg = cv2.imencode('.jpg', image) + return jpeg.tobytes() + +@application.route('/client/') +def client_list(): + json = clients + return jsonify(json) + +@application.route('/client//info') +def client_info(num): + json = clients[int(num)] + return jsonify(json) + +@application.route('/cam/') +def cam_list(): + json = cams + return jsonify(json) + +@application.route('/cam//info') +def cam_info(num): + json = cams[int(num)] + return jsonify(json) + +def gen(camera): + """Video streaming generator function.""" + while True: + frame = camera.get_frame() + yield (b'--frame\r\n' + b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') + +@application.route('/cam//stream') +def cam_stream(num): + + return Response(gen(VideoCamera(cams[int(num)]["ip"])), + mimetype='multipart/x-mixed-replace; boundary=frame') + +if __name__ == '__main__': + print(clients[0]) + application.run(host='127.0.0.1', port=80) diff --git a/mock/cams.json b/mock/cams.json new file mode 100644 index 0000000..0453241 --- /dev/null +++ b/mock/cams.json @@ -0,0 +1,20 @@ +[ + { + "id": 0, + "label": "cam1", + "ip": "http://208.72.70.171:80/mjpg/video.mjpg", + "status": true + }, + { + "id": 1, + "label": "cam2", + "ip": "http://89.29.108.38:80/mjpg/video.mjpg", + "status": false + }, + { + "id": 2, + "label": "cam3", + "ip": "http://62.99.80.154:81/mjpg/video.mjpg", + "status": true + } +] \ No newline at end of file diff --git a/mock/clients.json b/mock/clients.json new file mode 100644 index 0000000..6c7d2b6 --- /dev/null +++ b/mock/clients.json @@ -0,0 +1,20 @@ +[ + { + "id": 0, + "label": "name1", + "ip": "127.0.0.1", + "status": true + }, + { + "id": 1, + "label": "name2", + "ip": "127.0.0.1", + "status": false + }, + { + "id": 2, + "label": "name3", + "ip": "127.0.0.1", + "status": true + } +] \ No newline at end of file diff --git a/mock/requirements.txt b/mock/requirements.txt new file mode 100644 index 0000000..e3806c4 --- /dev/null +++ b/mock/requirements.txt @@ -0,0 +1,4 @@ +requests +flask +gunicorn +opencv-python \ No newline at end of file diff --git a/.gitignore b/server/.gitignore similarity index 84% rename from .gitignore rename to server/.gitignore index c531222..f4fa250 100644 --- a/.gitignore +++ b/server/.gitignore @@ -1,2 +1,2 @@ - -\.vscode/ + +\.vscode/ diff --git a/.pylintrc b/server/.pylintrc similarity index 100% rename from .pylintrc rename to server/.pylintrc diff --git a/README.md b/server/README.md similarity index 100% rename from README.md rename to server/README.md diff --git a/detector.py b/server/detector.py similarity index 91% rename from detector.py rename to server/detector.py index 4b004c9..84d514f 100644 --- a/detector.py +++ b/server/detector.py @@ -62,15 +62,17 @@ class Detector: def __init__(self, stream): self.model_path = "./model.pb" self.odapi = DetectorAPI(path_to_ckpt=self.model_path) - self.threshold = 0.3 + self.threshold = 0.8 self.stream = stream def detect(self): cap = cv2.VideoCapture(self.stream) r, img = cap.read() - img = cv2.resize(img, (500, 500)) - + if img is None: + return img + img = cv2.resize(img, (480, 270)) + boxes, scores, classes, num = self.odapi.processFrame(img) # Visualization of the results of a detection. @@ -82,13 +84,15 @@ class Detector: box = boxes[i] cv2.rectangle(img,(box[1],box[0]),(box[3],box[2]),(255,0,0),2) print("yes") - return True, img + + return img else: print("no") - return False, img - cv2.imshow("preview", img) # cv2.destroyWindow("preview") + return img + # cv2.imshow("preeview", img) # cv2.destroyWindow("preview") def __del__(self): self.cap.release() cv2.destroyAllWindows() + requests.get("http://192.168.178.53/stop") diff --git a/main.py b/server/main.py similarity index 65% rename from main.py rename to server/main.py index ad672b6..c51fa0e 100644 --- a/main.py +++ b/server/main.py @@ -4,28 +4,34 @@ import detector as dt import cv2 if __name__ == "__main__": - t = 30 # seconds a person can leave the room for + t = 1 # seconds a person can leave the room for t0 = time.time() time.clock() elapsed = 0 + #stream = "https://192.168.178.56:8080/video" stream = "http://217.128.254.187:8083/mjpg/video.mjpg" - detector = dt.Detector(stream) music_playing = False - + + #cv2.startWindowThread() + #cv2.namedWindow("preview") + while True: elapsed = time.time() - t0 if elapsed > t and music_playing: r = requests.get("http://192.168.178.53/stop") if r.status_code == 200: music_playing = False - - result, img = detector.detect() - if result and not music_playing: + tmp = time.time() + img = detector.detect() + print(time.time()-tmp) + if img is not None and not music_playing: r = requests.get("http://192.168.178.53/play") if r.status_code == 200: music_playing = True t0 = time.time() - cv2.imshow("preview", img) # cv2.destroyWindow("preview") - time.sleep(1) + + + cv2.imshow("preview", img) + cv2.waitKey(1) diff --git a/model.pb b/server/model.pb similarity index 100% rename from model.pb rename to server/model.pb diff --git a/motion_detector_old.py b/server/motion_detector_old.py similarity index 100% rename from motion_detector_old.py rename to server/motion_detector_old.py diff --git a/videos/example_01.mp4 b/server/videos/example_01.mp4 similarity index 100% rename from videos/example_01.mp4 rename to server/videos/example_01.mp4 diff --git a/videos/example_02.mp4 b/server/videos/example_02.mp4 similarity index 100% rename from videos/example_02.mp4 rename to server/videos/example_02.mp4