Pranjal-psytech commited on
Commit
9818966
1 Parent(s): ffd45c2

"Update file upload"

Browse files
Files changed (1) hide show
  1. app.py +31 -39
app.py CHANGED
@@ -14,55 +14,47 @@ CLASS_NAMES = ["Early Blight", "Late Blight", "Healthy"]
14
 
15
 
16
  # Define the function for making predictions
17
- def classify_image(image):
18
- # Open the image using Pillow
19
- img = Image.fromarray(image.astype('uint8'), 'RGB')
20
- new_size = (256, 256)
21
- img_resized = img.resize(new_size)
22
- #img_array = np.array(img_resized)
23
- #img_scaled = img_array / 255.0
24
- img_array = np.expand_dims(img_resized, axis=0)
 
 
 
 
25
  pred = model.predict(img_array)
26
 
27
  predicted_class = CLASS_NAMES[np.argmax(pred[0])]
28
  confidence = float(np.max(pred[0]))
29
 
30
  # Return the predicted class and confidence score
31
- return {"class": predicted_class, "confidence": confidence,"predict":pred[0]}
32
- # Resize the image to the desired size
33
- # new_size = (256, 256)
34
- # img_resized = image.resize(new_size)
35
-
36
- # # Convert the image to a numpy array
37
- # img_array = np.array(img_resized)
38
-
39
- # # Rescale the image pixel values to [0, 1]
40
- # img_scaled = img_array / 255.0
41
-
42
- # #img_array = np.array(img_scaled)
43
- # img_array = np.expand_dims(img_scaled, axis=0)
44
- # pred = model.predict(img_array)
45
-
46
- # # Display the rescaled image
47
- # #plt.imshow(img_array)
48
-
49
- # # Format the predicted class and confidence score
50
- # predicted_class = CLASS_NAMES[np.argmax(pred[0])]
51
- # confidence = float(np.max(pred[0]))
52
-
53
- # # Return the predicted class and confidence score
54
- # return {"class": predicted_class, "confidence": confidence,"predict":pred[0]}
55
- # img = (BytesIO(image))
56
- # img = img.resize((256, 256))
57
- # img_array = np.array(img)
58
- # img_array = np.expand_dims(img_array, axis=0)
59
- # pred = model.predict(img_array)
60
- # return pred[0][0]
61
 
62
  examples=[os.path.join(os.path.dirname(__file__),'7227b3db-c212-4370-8b42-443eea1577aa___RS_Early.B 7306.JPG','7456db33-766c-4a68-b924-ddf69d579981___RS_Early.B 6723.JPG','7486e823-64f7-4e43-ab51-26261b077fc2___RS_Early.B 6785.JPG','8829e413-5a7a-4680-b873-e71dfa9dbfe4___RS_LB 3974.JPG','9001b18c-b659-4c56-9dfb-0d0bf64a7b4a___RS_LB 4987.JPG','9009c86e-1205-4694-b0bb-ef7cf78dd104___RS_LB 3995.JPG','Potato_healthy-76-_0_2420.jpg','Potato_healthy-76-_0_6833.jpg','Potato_healthy-76-_0_7539.jpg')]
63
 
64
  # Define the Gradio interface for image input
65
- input_interface = gr.inputs.Image()
66
 
67
  # Define the Gradio interface for displaying the predicted class and confidence score
68
  output_interface = gr.Textbox()
 
14
 
15
 
16
  # Define the function for making predictions
17
+
18
+ def classify_image(file):
19
+ # Open the file using requests
20
+ response = requests.get(file)
21
+ img = Image.open(BytesIO(response.content))
22
+
23
+ # Resize and preprocess the image
24
+ img_resized = img.convert("RGB").resize((256, 256))
25
+ img_array = np.array(img_resized) / 255.0
26
+ img_array = np.expand_dims(img_array, axis=0)
27
+
28
+ # Make predictions
29
  pred = model.predict(img_array)
30
 
31
  predicted_class = CLASS_NAMES[np.argmax(pred[0])]
32
  confidence = float(np.max(pred[0]))
33
 
34
  # Return the predicted class and confidence score
35
+ return {"class": predicted_class, "confidence": confidence, "predict": pred[0]}
36
+
37
+ # def classify_image(image):
38
+ # # Open the image using Pillow
39
+ # img = Image.fromarray(image.astype('uint8'), 'RGB')
40
+ # new_size = (256, 256)
41
+ # img_resized = img.resize(new_size)
42
+ # #img_array = np.array(img_resized)
43
+ # #img_scaled = img_array / 255.0
44
+ # img_array = np.expand_dims(img_resized, axis=0)
45
+ # pred = model.predict(img_array)
46
+
47
+ # predicted_class = CLASS_NAMES[np.argmax(pred[0])]
48
+ # confidence = float(np.max(pred[0]))
49
+
50
+ # # Return the predicted class and confidence score
51
+ # return {"class": predicted_class, "confidence": confidence,"predict":pred[0]}
52
+
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
  examples=[os.path.join(os.path.dirname(__file__),'7227b3db-c212-4370-8b42-443eea1577aa___RS_Early.B 7306.JPG','7456db33-766c-4a68-b924-ddf69d579981___RS_Early.B 6723.JPG','7486e823-64f7-4e43-ab51-26261b077fc2___RS_Early.B 6785.JPG','8829e413-5a7a-4680-b873-e71dfa9dbfe4___RS_LB 3974.JPG','9001b18c-b659-4c56-9dfb-0d0bf64a7b4a___RS_LB 4987.JPG','9009c86e-1205-4694-b0bb-ef7cf78dd104___RS_LB 3995.JPG','Potato_healthy-76-_0_2420.jpg','Potato_healthy-76-_0_6833.jpg','Potato_healthy-76-_0_7539.jpg')]
55
 
56
  # Define the Gradio interface for image input
57
+ input_interface = gr.inputs.File(label="Upload an image file")
58
 
59
  # Define the Gradio interface for displaying the predicted class and confidence score
60
  output_interface = gr.Textbox()