forked from ManuelZ/tv_automation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_nuclio.py
79 lines (60 loc) · 2.12 KB
/
test_nuclio.py
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
78
79
# Standard Library imports
from pathlib import Path
import base64
import io
# External imports
import cv2
import numpy as np
from PIL import Image
import requests
# Note: Replace the port. Run `docker container ps` and look for the port being used
# by your Nuclio function container
ENDPOINT_URL = "http://localhost:57062"
def generate_dummy_image(width=100, height=100, color=(255, 0, 0)):
"""Generate a dummy image with a single color."""
image_array = np.zeros((height, width, 3), dtype=np.uint8)
image_array[:, :] = color
image = Image.fromarray(image_array)
return image
# Convert PIL image to Base64
def pil_to_base64(image):
buffered = io.BytesIO()
image.save(buffered, format="JPEG")
return base64.b64encode(buffered.getvalue()).decode("utf-8")
def pil_to_numpy(im):
im_numpy = np.array(im)
im_numpy = cv2.cvtColor(im_numpy, cv2.COLOR_BGR2RGB)
return im_numpy
# Send the image to the endpoint
def send_image_to_endpoint(image, endpoint_url, threshold=0.5):
image_data = pil_to_base64(image)
payload = {"image": image_data, "threshold": threshold}
response = requests.post(endpoint_url, json=payload)
if response.status_code == 200:
print("Image sent successfully.")
print("Response:", response.json())
return response.json()
else:
print("Failed to send image.")
print("Status Code:", response.status_code)
print("Response:", response.text)
im_path = Path("images/capture_example.png")
image = Image.open(im_path)
im_numpy = pil_to_numpy(image)
# Example usage
response = send_image_to_endpoint(image, ENDPOINT_URL)
for obj in response:
confidence = float(obj["confidence"])
label = obj["label"]
points = obj["points"]
# Convert float points to integer
x1, y1, x2, y2 = map(int, points)
# Draw rectangle
cv2.rectangle(im_numpy, (x1, y1), (x2, y2), (0, 255, 0), 2)
# Add label and confidence
text = f"{label} ({confidence:.2f})"
cv2.putText(
im_numpy, text, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2
)
cv2.imshow("results", im_numpy)
cv2.waitKey(0)