-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_request.py
More file actions
77 lines (65 loc) · 2.24 KB
/
http_request.py
File metadata and controls
77 lines (65 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import requests
import json
import os
import threading # UI 멈춤 방지용
import cv2
import base64
from dotenv import load_dotenv
load_dotenv()
SERVER_URL = os.getenv('SPRING_IP')
SERVER_PORT = os.getenv('SPRING_PORT')
def send_trash_status(trash_type, capacity):
data = {
"trashType": trash_type,
"capacity": capacity
}
try:
response = requests.post(
SERVER_URL,
headers={"Content-Type": "application/json"},
data=json.dumps(data)
)
if response.status_code == 201:
print("저장 완료, 저장된 알람의 Id : ", response.text)
else:
print("저장 실패 :", response.status_code, response.text)
except Exception as e:
print("API 호출에 문제가 발생했습니다. :", e)
# SpringBoot에 예측 결과를 전송하는 함수입니다.
def send_trash_result(result, img_base64):
"""
분류 결과와 Base64 이미지를 서버로 전송합니다.
"""
data = {
"classificationResult": result,
"img": img_base64
}
try:
# SERVER_URL이 None일 경우에 대한 예외처리 혹은 하드코딩 필요 시 수정
target_url = "http://"+SERVER_URL +":"+SERVER_PORT + "/api/raspberry/garbage/save"
response = requests.post(
target_url,
headers={"Content-Type": "application/json"},
data=json.dumps(data)
)
if response.status_code == 200:
print("저장 완료, ID:", response.text)
else:
print("저장 실패:", response.status_code, response.text)
except Exception as e:
print("API 호출 에러:", e)
# --- [추가] 스레드용 래퍼 함수 ---
def process_and_send_backend(label, frame):
"""
이미지를 Base64로 변환하고 전송 함수를 호출합니다.
(별도 스레드에서 실행됨)
"""
try:
# 1. OpenCV Frame -> JPG 인코딩
_, buffer = cv2.imencode('.jpg', frame)
# 2. Bytes -> Base64 String 변환
img_str = base64.b64encode(buffer).decode('utf-8')
# 3. 전송
send_trash_result(label, img_str)
except Exception as e:
print(f"이미지 변환/전송 중 오류: {e}")