Manith Marapperuma commited on
Commit
9355ddd
·
verified ·
1 Parent(s): a1e8b4e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -36
app.py CHANGED
@@ -6,78 +6,81 @@ from mtcnn import MTCNN
6
  import cv2
7
  import io
8
 
 
9
  # Load the model (ensure correct path for loading)
10
- # For Hugging Face Spaces, ensure the model file is included in the repository or loaded from a URL
11
-
12
-
13
- input_shape = (224, 224, 3)
14
- model = tf.keras.models.load_model('oily_dry.h5', compile=False, custom_objects={'input_shape': input_shape})
15
-
16
- # model = tf.keras.models.load_model('oily_dry.h5')
17
 
18
  # Load the MTCNN face detection model
19
  mtcnn = MTCNN()
20
 
21
- def detect_and_process_skin(image, output_folder=None):
22
- """Detects faces in an image, crops the skin region, and prepares it for classification."""
23
- # Convert PIL Image to numpy array
24
- img_np = np.array(image)
 
25
  img_rgb = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
26
 
27
  # Detect faces in the image
28
  detections = mtcnn.detect_faces(img_rgb)
29
 
 
30
  if detections:
31
  x, y, width, height = detections[0]['box']
32
 
33
  # Crop the face region
34
- face_img = img_np[y:y+height, x:x+width]
35
-
36
- # Convert cropped face to PIL Image for compatibility with model preprocessing
37
- pil_img = Image.fromarray(cv2.cvtColor(face_img, cv2.COLOR_BGR2RGB))
38
 
 
 
39
  return pil_img
40
  else:
41
- # Return the original image if no face was detected
42
- return image
43
 
44
- def classify_image(image):
45
- """Classifies the image after processing."""
46
- processed_image = detect_and_process_skin(image)
 
47
 
48
  # Resize and preprocess the image for the model
49
- processed_image = processed_image.resize((224, 224))
50
- image_array = tf.keras.preprocessing.image.img_to_array(processed_image) / 255.0
51
- image_array = np.expand_dims(image_array, axis=0)
52
 
53
  # Classify the image
54
- predictions = model.predict(image_array)
 
 
55
  predicted_class = np.argmax(predictions)
56
  percentages = predictions[0] * 100
 
57
 
58
- return predicted_class, *percentages
59
 
60
  def app():
61
  st.title("Oily/Dry Skin Level Predictor")
 
 
62
 
 
63
  image_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
64
 
 
65
  if image_file is not None:
66
- # Convert the file to an image
67
- image = Image.open(image_file)
68
- predicted_class, dry_percentage, oily_percentage, normal_percentage = classify_image(image)
69
 
70
  # Convert processed image to bytes for display
71
- processed_image_bytes = io.BytesIO()
72
- image.save(processed_image_bytes, format='JPEG')
73
- processed_image_bytes.seek(0)
74
-
75
- st.image(processed_image_bytes, width=250)
76
 
77
- # Display the classification results
 
78
  st.write(f"Dry Skin: {dry_percentage:.2f}%")
 
79
  st.write(f"Oily Skin: {oily_percentage:.2f}%")
 
80
  st.write(f"Normal Skin: {normal_percentage:.2f}%")
81
 
82
- if __name__ == "__main__":
83
- app()
 
6
  import cv2
7
  import io
8
 
9
+ # Assuming model is included in your repository or loaded from a URL
10
  # Load the model (ensure correct path for loading)
11
+ model = tf.keras.models.load_model('oily_dry.h5')
 
 
 
 
 
 
12
 
13
  # Load the MTCNN face detection model
14
  mtcnn = MTCNN()
15
 
16
+ def detect_and_process_skin(image_bytes):
17
+ """Detects faces in an image, crops the skin region, and returns it as an image object."""
18
+ # Load image from bytes
19
+ img = Image.open(io.BytesIO(image_bytes))
20
+ img_np = np.array(img)
21
  img_rgb = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
22
 
23
  # Detect faces in the image
24
  detections = mtcnn.detect_faces(img_rgb)
25
 
26
+ # Check if any faces were detected
27
  if detections:
28
  x, y, width, height = detections[0]['box']
29
 
30
  # Crop the face region
31
+ face_img_np = img_np[y:y+height, x:x+width]
 
 
 
32
 
33
+ # Convert to PIL Image for return
34
+ pil_img = Image.fromarray(face_img_np)
35
  return pil_img
36
  else:
37
+ # Return original image if no face was detected
38
+ return img
39
 
40
+ def classify_image(image_bytes):
41
+ """Loads, preprocess, and classifies the image from bytes."""
42
+ # Process the image using face detection
43
+ pil_img = detect_and_process_skin(image_bytes)
44
 
45
  # Resize and preprocess the image for the model
46
+ image = pil_img.resize((224, 224))
47
+ image = tf.keras.preprocessing.image.img_to_array(image) / 255.0
48
+ image = np.expand_dims(image, axis=0)
49
 
50
  # Classify the image
51
+ predictions = model.predict(image)
52
+
53
+ # Process predictions for clarity
54
  predicted_class = np.argmax(predictions)
55
  percentages = predictions[0] * 100
56
+ dry_percentage, normal_percentage, oily_percentage = percentages
57
 
58
+ return predicted_class, dry_percentage, oily_percentage, normal_percentage
59
 
60
  def app():
61
  st.title("Oily/Dry Skin Level Predictor")
62
+ st.write("Coded by Manith Jayaba")
63
+ st.write("This app can measure the oiliness and dryness of your skin")
64
 
65
+ # Get the image file
66
  image_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
67
 
68
+ # Classify and display the result
69
  if image_file is not None:
70
+ predicted_class, dry_percentage, oily_percentage, normal_percentage = classify_image(image_file.getvalue())
 
 
71
 
72
  # Convert processed image to bytes for display
73
+ img_bytes = io.BytesIO()
74
+ detect_and_process_skin(image_file.getvalue()).save(img_bytes, format='JPEG')
75
+ st.image(img_bytes.getvalue(), width=250, caption="Processed Image")
 
 
76
 
77
+ # Display progress bars
78
+ st.progress(int(dry_percentage))
79
  st.write(f"Dry Skin: {dry_percentage:.2f}%")
80
+ st.progress(int(oily_percentage))
81
  st.write(f"Oily Skin: {oily_percentage:.2f}%")
82
+ st.progress(int(normal_percentage))
83
  st.write(f"Normal Skin: {normal_percentage:.2f}%")
84
 
85
+ if _name_ == "_main_":
86
+ app()