Image capture using Python - Flask application programming interface(API)














































Image capture using Python - Flask application programming interface(API)



Code to record the screen for a duration of 60 seconds. The recording is saved in the form of frames in .jpg format into the destination file. Flask is used to build web-application in the code.
The project requires libraries like Flask, cv2 library from OpenCV used to perform image processing, datetime for creating stamps and storing the .jpg files without replacement for a duration of 60 seconds.

#Sadasivuni Durga
from flask import Flask, render_template, Response, request
import cv2 import datetime, time global capture,rec_frame,rec, out capture=0 switch=1 rec=0 #instatiate flask app global app app = Flask(__name__, template_folder='./templates') camera = cv2.VideoCapture(0) # record with interval of 0.05 seconds def record(out): while(rec): time.sleep(0.05) out.write(rec_frame) # generate frames def gen_frames(): global out, rec_frame # generate frames and read it while True: success, frame = camera.read() if success: if(rec): rec_frame=frame frame= cv2.putText(cv2.flip(frame,1),"Shots begin...", (0,25), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255),4) frame=cv2.flip(frame,1) try: ret, buffer = cv2.imencode('.jpg', cv2.flip(frame,1)) frame = buffer.tobytes() yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') except Exception as e: pass else: pass # index.html is required to render template @app.route('/') def index(): return render_template('index.html') @app.route('/video_feed') def video_feed(): return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame') # GET or POST requests made to HTML @app.route('/requests',methods=['POST','GET']) def tasks(): global out if request.method == 'POST': if request.form.get('rec') == 'Start Recording': global rec rec= not rec capture_duration=1 cap = cv2.VideoCapture(0) rec, frame = cap.read() start_time = time.time() count=0 if(rec): while( int(time.time() - start_time) < capture_duration ): rec, frame = cap.read() now=datetime.datetime.now() out = cv2.imwrite('img_{}.jpg'.format(str(now).replace(":",'')),frame) count+=1 camera.release() return 'Task Done!' # main program if __name__ == '__main__': app.run()

The "index.html" file plays the front end role part. The visible webpage needs to get data from the camera and save the same to the destination folder. The function of getting data from the camera is done by "POST" method using a submit button named " Start Recording". The entire HTML code is depicted below:

<body> <div class="container"> <div class="row"> <div class="col-lg-8 offset-lg-2"> <form method="post" action="{{ url_for('tasks') }}"> <input type="submit" value="Start Recording" name="rec" /> </form> <img src="{{ url_for('video_feed') }}" height="60%"> <li>Start Recording..</li> </div> </div> </div> </body>

 The output on python IDLE running is shown below

Copy and paste the URL into the web browser. This will redirect to index.html page.

Once you click the start recording  button, the screen will be recorded and output is saved in the form of frames.