Change roboflow package to REST API, update requirements

This commit is contained in:
Piotr Szkudlarek 2024-01-15 00:41:24 +01:00
parent 024057d2cc
commit 8715bffbe4
2 changed files with 39 additions and 20 deletions

View File

@ -1,14 +1,46 @@
from django.conf import settings
from roboflow import Roboflow
from PIL import Image, ImageDraw
import os
import requests
import base64
rf = Roboflow(api_key=os.environ["API_KEY_ROBO"])
model = rf.workspace().project("plankton-vhsho").version(1).model
url = "https://detect.roboflow.com/plankton-vhsho/1"
api_key = os.environ["API_KEY_ROBO"]
def predict_image(image):
results = model.predict(image.image.path)
results.save(
f"{settings.MEDIA_ROOT}/{image.image.name.split('.')[0]}_predicted.{image.image.name.split('.')[-1]}"
with open(image.image.path, "rb") as f:
enc_image = base64.b64encode(f.read())
results = requests.post(
url=url,
params={"api_key": api_key},
data=enc_image,
headers={"Content-Type": "application/x-www-form-urlencoded"},
)
for box in results.json()["predictions"]:
save_image(box, image)
return results.json()
def save_image(box, source_img):
x1 = box["x"] - box["width"] / 2
x2 = box["x"] + box["width"] / 2
y1 = box["y"] - box["height"] / 2
y2 = box["y"] + box["height"] / 2
bounding_box = ((x1, y1), (x2, y2))
class_ = box["class"]
source = Image.open(source_img.image.path).convert("RGB")
draw = ImageDraw.Draw(source)
text_pos = (bounding_box[0][0] + 5, bounding_box[0][1] + 5)
left, top, right, bottom = draw.textbbox(text_pos, class_, font_size=15)
draw.rectangle((left, top - 4, right + 5, bottom + 5), fill="blue")
draw.text(text_pos, class_, color="white", font_size=15)
draw.rectangle(bounding_box, width=5, outline="blue")
source.save(
f"{settings.MEDIA_ROOT}/{source_img.image.name.split('.')[0]}_predicted.{source_img.image.name.split('.')[-1]}",
"JPEG",
)

View File

@ -14,12 +14,9 @@ bleach==6.1.0
cachetools==5.3.2
certifi==2023.7.22
cffi==1.16.0
chardet==4.0.0
charset-normalizer==3.3.2
click==8.1.7
comm==0.1.4
contourpy==1.2.0
cycler==0.10.0
debugpy==1.8.0
decorator==5.1.1
defusedxml==0.7.1
@ -31,7 +28,6 @@ environ==1.0
exceptiongroup==1.1.3
executing==2.0.0
fastjsonschema==2.19.0
fonttools==4.45.0
fqdn==1.5.1
ipykernel==6.25.2
ipython==8.16.1
@ -56,9 +52,7 @@ jupyterlab==4.0.9
jupyterlab-widgets==3.0.9
jupyterlab_pygments==0.3.0
jupyterlab_server==2.25.2
kiwisolver==1.4.5
MarkupSafe==2.1.3
matplotlib==3.8.2
matplotlib-inline==0.1.6
mistune==3.0.2
mypy-extensions==1.0.0
@ -70,7 +64,6 @@ notebook==7.0.6
notebook_shim==0.2.3
numpy==1.26.0
opencv-python==4.9.0.80
opencv-python-headless==4.8.0.74
overrides==7.4.0
packaging==23.2
pandocfilters==1.5.0
@ -78,7 +71,7 @@ parso==0.8.3
pathspec==0.11.2
pexpect==4.8.0
pickleshare==0.7.5
Pillow==10.1.0
pillow==10.2.0
pip-autoremove==0.10.0
platformdirs==3.11.0
prometheus-client==0.19.0
@ -95,19 +88,15 @@ pycparser==2.21
Pygments==2.16.1
pyparsing==3.1.1
python-dateutil==2.8.2
python-dotenv==1.0.0
python-json-logger==2.0.7
python-magic==0.4.27
PyYAML==6.0.1
pyzmq==25.1.1
qtconsole==5.5.1
QtPy==2.4.1
referencing==0.31.1
requests==2.31.0
requests-toolbelt==1.0.0
rfc3339-validator==0.1.4
rfc3986-validator==0.1.1
roboflow==1.1.16
rpds-py==0.13.2
rsa==4.9
scikit-learn==1.3.1
@ -119,13 +108,11 @@ soupsieve==2.5
split-folders==0.5.1
sqlparse==0.4.4
stack-data==0.6.3
supervision==0.17.1
terminado==0.18.0
threadpoolctl==3.2.0
tinycss2==1.2.1
tomli==2.0.1
tornado==6.3.3
tqdm==4.66.1
traitlets==5.11.2
types-python-dateutil==2.8.19.14
typing_extensions==4.8.0