pipecounting / main.py
bankba's picture
Upload 8 files
ecac36d
Raw History Blame Contribute Delete
4.38 kB
from model import get_yolov5
from fastapi import FastAPI, File, UploadFile
from io import BytesIO
from fastapi.responses import Response
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
import cv2
import json
import numpy as np
app = FastAPI(title="PIPE COUNTING API",
description="""Upload PIPE image and the API will response number of pipe""",
version="0.0.1",)
# Mount the "static" folder to serve static files (like your index.html)
app.mount("/static", StaticFiles(directory="static"), name="static")
# Define a route to serve the index.html file
@app.get("/")
async def get_index():
return FileResponse("static/index.html")
## สร้าง object ของโมเดลไว้สำหรับการเช็คโลโก้ในภาพ
@app.post("/detectImage")
async def detect_image(file: UploadFile):
model_pipe = get_yolov5(0.5)
# Load your image using OpenCV
image_data = await file.read()
image_np = np.frombuffer(image_data, np.uint8)
original_img = cv2.imdecode(image_np, cv2.IMREAD_COLOR)
# Inference with your model
results = model_pipe(original_img, size=4096)
#results.render(labels=True)
data = results.pandas().xyxy[0]
# Calculate the total count and display it in the top bar
total_count = len(data)
font_scale = 1
font_thickness = 1
font_color = (0, 0, 0) # Black color
background_color = (0, 0, 0) # Black background color
totals_text = f"Pipe Count = {total_count}"
# Loop through the results and plot text with row numbers and a stroke inside each object
for idx, row in data.iterrows():
xmin, ymin, xmax, ymax = int(row['xmin']), int(row['ymin']), int(row['xmax']), int(row['ymax'])
center_x = (xmin + xmax) // 2
center_y = (ymin + ymax) // 2
text = str(idx + 1) # Adding 1 to start row numbers from 1
# Calculate the font size based on the size of the bounding box
font_scale = min((xmax - xmin), (ymax - ymin)) / 150 # Adjust the divisor as needed
# Calculate the position to center the text
text_size, _ = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, font_scale, font_thickness)
text_x = center_x - text_size[0] // 2
text_y = center_y + text_size[1] // 2
# Draw a white circle behind the text
circle_radius = int(font_scale * 50) # Adjust the radius as needed
cv2.circle(
original_img,
(center_x, center_y),
circle_radius,
(255, 255, 255, 128), # White color with 50% opacity (128 alpha)
thickness=-1 # Negative thickness fills the circle
)
# Draw the circle border
border_thickness = 2 # Adjust the thickness of the border
cv2.circle(
original_img,
(center_x, center_y),
circle_radius,
(255, 0, 0), # Black color for the border
thickness=border_thickness
)
# Draw the text on the image
cv2.putText(original_img, text, (text_x, text_y), cv2.FONT_HERSHEY_SIMPLEX, font_scale, font_color, font_thickness)
# Create a top bar and add the text to it
# Calculate the height of the top bar based on a percentage of the original image's height
percentage_of_height = 0.1 # You can adjust this percentage as needed
top_bar_height = max(int(original_img.shape[0] * percentage_of_height),50)
# Calculate the font size based on the top bar height (you can adjust this factor)
font_scale = max(top_bar_height // 50, 1) # Ensure font_scale is at least 1
top_bar = np.zeros((top_bar_height, original_img.shape[1], 3), dtype=np.uint8)
top_bar = cv2.rectangle(top_bar, (0, 0), (original_img.shape[1], top_bar_height), background_color, -1)
cv2.putText(top_bar, totals_text, (10, top_bar_height-font_scale*15), cv2.FONT_HERSHEY_SIMPLEX, font_scale, (255,255,255), font_thickness+1)
# Combine the top bar with the original image
combined_img = np.vstack((top_bar, original_img))
# Convert the combined image back to bytes
_, img_bytes = cv2.imencode('.jpg', combined_img)
img_bytes_io = BytesIO(img_bytes.tobytes())
# Return the modified image as a response
return Response(content=img_bytes_io.getvalue(), media_type="image/jpeg")