kcuric / surveillance-system

DIY surveillance system with a face detection implemented with Python, OpenCV and Flask. Made as a team project for a System Security course at the Faculty of Organization and Informatics, Varaždin, Croatia.
3 stars 0 forks source link

Implement the Flask web app #9

Open kcuric opened 4 years ago

kcuric commented 4 years ago

Implement the Flask web application that will be running on the created GCP VM Instance.

mkristofic commented 4 years ago

Implemented testing environment on local network.

# app.py

from flask import Flask, render_template, Response
import socket
import numpy as np
import cv2

host = "192.168.1.4"
port = 5005
server_address = (host, port)

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(server_address)
print('starting up on %s:%s' % server_address)

app = Flask(__name__)

def gen():
    while True:
        data, address = sock.recvfrom(65535)
        # nparr = np.frombuffer(data, np.uint8)
        # img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + data + b'\r\n')

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/video_feed')
def video_feed():
    return Response(gen(), mimetype='multipart/x-mixed-replace; boundary=frame')

if __name__ == '__main__':
    app.run(ssl_context='adhoc')
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>DIY Survailence System</title>
</head>
<body>
    <h1>DIY Survailence System</h1>
    <p>Running the app.py</p>
    <img src="{{ url_for('video_feed') }}">
</html>
mkristofic commented 4 years ago

Regarding the previous commit, without the option to reuse the addres, when trying to run the server, an error pops up saying that the address is already in use (shown in the image below).

image

kcuric commented 4 years ago

To run the client script user now must provide arguments.

Needed arguments for the client script:

  1. port number (mandatory)
  2. camera number (optional)

If the user uses only one camera than providing the camera number is not necessary.