11"""API v1 route handlers."""
22
3- from fastapi import APIRouter , Response
3+ from fastapi import APIRouter , Response , File , UploadFile
4+ import numpy as np
5+ from fastapi .responses import StreamingResponse
46from typing import Dict , List
5- from src .modules .transporter import publish_message
7+ from src .modules .transporter import add_to_queue
68from src .modules .models .index import embed_text
79import io
810# Create v1 router
911router = APIRouter (prefix = '/v1' , tags = ['v1' ])
1012import matplotlib .pyplot as plt
13+ from src .modules .transporter .redis_client import pubsub
14+ from src .modules .transporter .kafka import kafka_pubsub
1115
1216@router .get ("/hello" )
1317async def hello_world ():
1418 """Hello world endpoint."""
15- publish_message ("hello-python" , "Hello from FastAPI!" )
16- embed = embed_text ( "Hello, world! " )
17- print ( f"Generated embedding: { embed } " )
18- return {"message" : "Hello, reloaded!" , "embedding" : list ( embed ) }
19+ add_to_queue ("hello-python" , "Hello from FastAPI!" )
20+ await pubsub . publish ( 'chat' , "message " )
21+ await kafka_pubsub . publish ( 'chat' , "messagejbdjchsjdhcjsdchbsjdch " )
22+ return {"message" : "Hello, reloaded!" }
1923
2024@router .get ("/health" )
21- async def health_check () -> Dict [str , str ]:
25+ async def health_check (msg ) -> Dict [str , str ]:
26+ print ("[chat rehealthdis] Received: message" )
2227 """Health check endpoint."""
2328 return {"status" : "healthy" }
2429
@@ -49,4 +54,58 @@ async def get_plot():
4954 buf .seek (0 )
5055
5156 # Trả về dưới dạng ảnh PNG
52- return Response (content = buf .getvalue (), media_type = "image/png" )
57+ return Response (content = buf .getvalue (), media_type = "image/png" )
58+
59+ import cv2
60+ @router .post ("/edit-image/" )
61+ async def edit_image (file : UploadFile = File (...)):
62+ import cv2
63+ # Đọc file ảnh từ request
64+ contents = await file .read ()
65+ nparr = np .frombuffer (contents , np .uint8 )
66+ img = cv2 .imdecode (nparr , cv2 .IMREAD_COLOR )
67+
68+ # Xử lý ảnh: ví dụ chuyển sang ảnh xám
69+ gray = cv2 .cvtColor (img , cv2 .COLOR_BGR2GRAY )
70+
71+ # Chuyển lại thành ảnh màu để trả về (nếu cần)
72+ result = cv2 .cvtColor (gray , cv2 .COLOR_GRAY2BGR )
73+
74+ # Encode ảnh thành bytes
75+ _ , img_encoded = cv2 .imencode ('.jpg' , result )
76+ return StreamingResponse (io .BytesIO (img_encoded .tobytes ()), media_type = "image/jpeg" )
77+
78+
79+ # Load bộ phân loại khuôn mặt Haar Cascade
80+ face_cascade = cv2 .CascadeClassifier (
81+ cv2 .data .haarcascades + "haarcascade_frontalface_default.xml"
82+ )
83+
84+ @router .post ("/detect-faces/" )
85+ async def detect_faces (file : UploadFile = File (...)):
86+ # Đọc dữ liệu ảnh
87+ contents = await file .read ()
88+ nparr = np .frombuffer (contents , np .uint8 )
89+ img = cv2 .imdecode (nparr , cv2 .IMREAD_COLOR )
90+
91+ # Chuyển sang ảnh xám để nhận diện
92+ gray = cv2 .cvtColor (img , cv2 .COLOR_BGR2GRAY )
93+
94+ # Phát hiện khuôn mặt
95+ faces = face_cascade .detectMultiScale (
96+ gray ,
97+ scaleFactor = 1.1 ,
98+ minNeighbors = 5 ,
99+ minSize = (30 , 30 )
100+ )
101+
102+ # Vẽ hình chữ nhật quanh khuôn mặt
103+ for (x , y , w , h ) in faces :
104+ cv2 .rectangle (img , (x , y ), (x + w , y + h ), (0 , 255 , 0 ), 2 )
105+
106+ # Chuyển ảnh kết quả thành bytes
107+ _ , img_encoded = cv2 .imencode ('.jpg' , img )
108+ return StreamingResponse (
109+ io .BytesIO (img_encoded .tobytes ()),
110+ media_type = "image/jpeg"
111+ )
0 commit comments