import cv2 import mediapipe as mp import json import time from flask import Flask, Response import sys max_readings = 10 cameraIndex = sys.argv[1] porta = sys.argv[2] url = sys.argv[3] arquivoSaida = sys.argv[4] app = Flask(__name__) cap = cv2.VideoCapture(cameraIndex) mp_face_detection = mp.solutions.face_detection face_detection = mp_face_detection.FaceDetection(min_detection_confidence=0.5) readings = [] def generate_frames(): while True: ret, frame = cap.read() frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) results = face_detection.process(frame_rgb) current_readings = [] if results.detections: for idx, detection in enumerate(results.detections): bboxC = detection.location_data.relative_bounding_box ih, iw, _ = frame.shape x, y, w, h = int(bboxC.xmin * iw), int(bboxC.ymin * ih), int(bboxC.width * iw), int(bboxC.height * ih) current_readings.append({'id': idx, 'x': x, 'y': y, 'largura': w, 'altura': h}) cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2) cv2.putText(frame, f'Rosto {idx}', (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2) timestamp = time.time() readings.append({'timestamp': timestamp, 'rostos': current_readings}) if len(readings) > max_readings: readings.pop(0) if len(readings) == max_readings: try: with open('scripts/' + arquivoSaida, 'w') as file: json.dump({'frames': readings}, file) except Exception as e: print(f"Erro ao escrever no arquivo: {e}") ret, buffer = cv2.imencode('.jpg', frame) frame = buffer.tobytes() yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') @app.route('/' + url) def video_feed(): return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame') if __name__ == "__main__": # Acessando os argumentos passados na linha de comando if len(sys.argv) != 4: print("Usage: python script.py ") sys.exit(1) app.run(host='0.0.0.0', port=int(porta))