Neurolingua commited on
Commit
e18a400
1 Parent(s): 36ec663

Upload other_function.py

Browse files
Files changed (1) hide show
  1. other_function.py +64 -0
other_function.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ from requests.auth import HTTPBasicAuth
4
+ from PIL import Image
5
+ from io import BytesIO
6
+ from urllib.parse import urlparse
7
+ import os
8
+
9
+
10
+ from inference_sdk import InferenceHTTPClient
11
+ import base64
12
+ UPLOAD_FOLDER = '/code/uploads'
13
+ if not os.path.exists(UPLOAD_FOLDER):
14
+ os.makedirs(UPLOAD_FOLDER)
15
+
16
+ def predict_pest(filepath):
17
+ CLIENT = InferenceHTTPClient(
18
+ api_url="https://detect.roboflow.com",
19
+ api_key="oF1aC4b1FBCDtK8CoKx7"
20
+ )
21
+ result = CLIENT.infer(filepath, model_id="pest-detection-ueoco/1")
22
+ return result['predicted_classes'][0]
23
+
24
+
25
+ def predict_disease(filepath):
26
+ CLIENT = InferenceHTTPClient(
27
+ api_url="https://classify.roboflow.com",
28
+ api_key="oF1aC4b1FBCDtK8CoKx7"
29
+ )
30
+ result = CLIENT.infer(filepath, model_id="plant-disease-detection-iefbi/1")
31
+ return result['predicted_classes'][0]
32
+
33
+ def convert_img(url, account_sid, auth_token):
34
+ try:
35
+ # Make the request to the media URL with authentication
36
+ response = requests.get(url, auth=HTTPBasicAuth(account_sid, auth_token))
37
+ response.raise_for_status() # Raise an error for bad responses
38
+
39
+ # Determine a filename from the URL
40
+ parsed_url = urlparse(url)
41
+ media_id = parsed_url.path.split('/')[-1] # Get the last part of the URL path
42
+ filename = f"downloaded_media_{media_id}"
43
+
44
+ # Save the media content to a file
45
+ media_filepath = os.path.join(UPLOAD_FOLDER, filename)
46
+ with open(media_filepath, 'wb') as file:
47
+ file.write(response.content)
48
+
49
+ print(f"Media downloaded successfully and saved as {media_filepath}")
50
+
51
+ # Convert the saved media file to an image
52
+ with open(media_filepath, 'rb') as img_file:
53
+ image = Image.open(img_file)
54
+
55
+ # Optionally, convert the image to JPG and save in UPLOAD_FOLDER
56
+ converted_filename = f"image.jpg"
57
+ converted_filepath = os.path.join(UPLOAD_FOLDER, converted_filename)
58
+ image.convert('RGB').save(converted_filepath, 'JPEG')
59
+ return converted_filepath
60
+
61
+ except requests.exceptions.HTTPError as err:
62
+ print(f"HTTP error occurred: {err}")
63
+ except Exception as err:
64
+ print(f"An error occurred: {err}")