83 lines
3.0 KiB
Python
83 lines
3.0 KiB
Python
import cv2
|
|
import numpy as np
|
|
from flask import Flask, Response, request
|
|
import json
|
|
import time
|
|
import sys
|
|
import os
|
|
|
|
|
|
max_readings = int(sys.argv[1])
|
|
porta = sys.argv[2]
|
|
url = sys.argv[3]
|
|
arquivoSaida = sys.argv[4]
|
|
|
|
app = Flask(__name__)
|
|
# Função para detecção de objetos usando YOLO e salvar as coordenadas em um arquivo JSON
|
|
def detect_objects(conf_threshold, nms_threshold, _camera_index):
|
|
# Definir intervalos de cor verde no espaço HSV
|
|
lower_green = np.array([35, 50, 50]) # Valores de limite inferior (Hue, Saturation, Value)
|
|
upper_green = np.array([90, 255, 255]) # Valores de limite superior (Hue, Saturation, Value)
|
|
|
|
cap = cv2.VideoCapture(_camera_index, cv2.CAP_DSHOW)
|
|
readings = []
|
|
|
|
while True:
|
|
ret, frame = cap.read()
|
|
if not ret:
|
|
break
|
|
|
|
# Converter a imagem de BGR para HSV
|
|
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
|
|
|
|
# Criar uma máscara para os pixels verdes na faixa especificada
|
|
mask = cv2.inRange(hsv, lower_green, upper_green)
|
|
|
|
# Encontrar os contornos na máscara
|
|
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
|
|
|
current_readings = []
|
|
idx = 0 # Índice de detecção
|
|
for contour in contours:
|
|
area = cv2.contourArea(contour)
|
|
if area > 1000: # Filtrar áreas menores que um valor específico (ajuste conforme necessário)
|
|
x, y, w, h = cv2.boundingRect(contour)
|
|
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
|
cv2.putText(frame, f'Green {idx}', (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
|
|
|
# Salvar as informações da detecção
|
|
detection_info = {
|
|
'id': int(idx),
|
|
'descricao': 'Green',
|
|
'x': int(x),
|
|
'y': int(y),
|
|
'largura': int(w),
|
|
'altura': int(h),
|
|
'confianca': 1.0 # Neste caso, confiança fixa para detecções baseadas em cores
|
|
}
|
|
current_readings.append(detection_info)
|
|
idx += 1
|
|
|
|
# Atualizar os resultados
|
|
timestamp = time.time()
|
|
readings.append({'timestamp': timestamp, 'objetos': current_readings})
|
|
|
|
# ... (código para salvar em arquivo e mostrar a imagem permanece inalterado)
|
|
|
|
# Convertendo frame para JPEG e enviando via Flask
|
|
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, methods=['GET'])
|
|
def video_feed():
|
|
conf_threshold = float(request.args.get('conf_threshold'))
|
|
nms_threshold = float(request.args.get('nms_threshold'))
|
|
camera_index = int(request.args.get('camera_index'))
|
|
return Response(detect_objects(conf_threshold, nms_threshold, camera_index), mimetype='multipart/x-mixed-replace; boundary=frame')
|
|
|
|
if __name__ == "__main__":
|
|
app.run(host='0.0.0.0', port=porta)
|