Spaces:
Sleeping
Sleeping
File size: 2,333 Bytes
e18a400 |
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 |
import os
import requests
from requests.auth import HTTPBasicAuth
from PIL import Image
from io import BytesIO
from urllib.parse import urlparse
import os
from inference_sdk import InferenceHTTPClient
import base64
UPLOAD_FOLDER = '/code/uploads'
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
def predict_pest(filepath):
CLIENT = InferenceHTTPClient(
api_url="https://detect.roboflow.com",
api_key="oF1aC4b1FBCDtK8CoKx7"
)
result = CLIENT.infer(filepath, model_id="pest-detection-ueoco/1")
return result['predicted_classes'][0]
def predict_disease(filepath):
CLIENT = InferenceHTTPClient(
api_url="https://classify.roboflow.com",
api_key="oF1aC4b1FBCDtK8CoKx7"
)
result = CLIENT.infer(filepath, model_id="plant-disease-detection-iefbi/1")
return result['predicted_classes'][0]
def convert_img(url, account_sid, auth_token):
try:
# Make the request to the media URL with authentication
response = requests.get(url, auth=HTTPBasicAuth(account_sid, auth_token))
response.raise_for_status() # Raise an error for bad responses
# Determine a filename from the URL
parsed_url = urlparse(url)
media_id = parsed_url.path.split('/')[-1] # Get the last part of the URL path
filename = f"downloaded_media_{media_id}"
# Save the media content to a file
media_filepath = os.path.join(UPLOAD_FOLDER, filename)
with open(media_filepath, 'wb') as file:
file.write(response.content)
print(f"Media downloaded successfully and saved as {media_filepath}")
# Convert the saved media file to an image
with open(media_filepath, 'rb') as img_file:
image = Image.open(img_file)
# Optionally, convert the image to JPG and save in UPLOAD_FOLDER
converted_filename = f"image.jpg"
converted_filepath = os.path.join(UPLOAD_FOLDER, converted_filename)
image.convert('RGB').save(converted_filepath, 'JPEG')
return converted_filepath
except requests.exceptions.HTTPError as err:
print(f"HTTP error occurred: {err}")
except Exception as err:
print(f"An error occurred: {err}") |