HashamUllah commited on
Commit
25926f8
·
verified ·
1 Parent(s): a7fef5f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -20
app.py CHANGED
@@ -10,29 +10,23 @@ model = load_model('cnn_model.h5')
10
  label_binarizer = pickle.load(open('label_transform.pkl', 'rb'))
11
 
12
  # Function to convert images to array
13
- def convert_image_to_array(image):
14
  try:
15
- # Debugging: Print image type and size
16
- print(f"Image type: {type(image)}")
17
- print(f"Image size: {len(image)} bytes")
18
-
19
- image = cv2.imdecode(np.frombuffer(image, np.uint8), cv2.IMREAD_COLOR)
20
-
21
  if image is not None:
22
- print(f"Decoded image shape: {image.shape}")
23
- image = cv2.resize(image, (256, 256))
24
- print(f"Resized image shape: {image.shape}")
25
  return img_to_array(image)
26
  else:
27
- print("Image decoding returned None")
28
  return np.array([])
29
  except Exception as e:
30
- print(f"Error: {e}")
31
  return None
32
 
33
- def predict_image(image):
34
  try:
35
- image_array = convert_image_to_array(image)
 
 
36
 
37
  if image_array.size == 0:
38
  return "Invalid image"
@@ -51,14 +45,15 @@ def predict_image(image):
51
  except Exception as e:
52
  return str(e)
53
 
54
- # Define Gradio interface
55
- interface = gr.Interface(
56
- fn=predict_image,
57
- inputs=gr.Image(type="numpy"),
58
  outputs="text",
59
  title="Image Classification",
60
- description="Upload an image to get the predicted class."
61
  )
62
 
 
63
  if __name__ == "__main__":
64
- interface.launch(share=True)
 
10
  label_binarizer = pickle.load(open('label_transform.pkl', 'rb'))
11
 
12
  # Function to convert images to array
13
+ def convert_image_to_array(image_dir):
14
  try:
15
+ image = cv2.imdecode(np.frombuffer(image_dir, np.uint8), cv2.IMREAD_COLOR)
 
 
 
 
 
16
  if image is not None:
17
+ image = cv2.resize(image, (256, 256))
 
 
18
  return img_to_array(image)
19
  else:
 
20
  return np.array([])
21
  except Exception as e:
22
+ print(f"Error : {e}")
23
  return None
24
 
25
+ def predict(image):
26
  try:
27
+ # Convert the image to an array
28
+ _, image_data = cv2.imencode('.jpg', image)
29
+ image_array = convert_image_to_array(image_data.tobytes())
30
 
31
  if image_array.size == 0:
32
  return "Invalid image"
 
45
  except Exception as e:
46
  return str(e)
47
 
48
+ # Create a Gradio interface
49
+ iface = gr.Interface(
50
+ fn=predict,
51
+ inputs=gr.inputs.Image(type="numpy", label="Upload an image"),
52
  outputs="text",
53
  title="Image Classification",
54
+ description="Upload an image to classify it."
55
  )
56
 
57
+ # Launch the interface
58
  if __name__ == "__main__":
59
+ iface.launch()