-1

I am using the below code to detect dark vs well-lit images but it is not giving accurate results and is giving very long floating values like .00000000123 etc.

#!/usr/bin/env python3
import cv2
import tensorflow as tf
import numpy as np
import os

path = '/home/..../Downloads/normal_images'
files = os.listdir(path)
# Load the pre-trained MobileNetV2 model
model = tf.keras.applications.MobileNetV2(weights='imagenet')
x =  np.float32(0.5)

for file_name in files:
    file_path = os.path.join(path, file_name)
# Load an image to classify

    img = cv2.imread(file_path)
    img = cv2.resize(img, (224, 224))
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    np.set_printoptions(suppress=True, precision=20)
    img = np.expand_dims(img, axis=0)
    probs = model.predict(img)
    prob_val = probs[0][0]
    if (prob_val > x):
            not_dark +=1
    else:
            dark +=1

print(f"No.of dark images identified: {dark}")
print(f"No.of normal images identified: {not_dark}")

Is my approach correct ?

  • I might be mistaken since I'm not familiar with the keras models, but isn't `MobileNetV2(weights="imagenet")` used for classifying images into categories based on the image content? I don't see any reason why it would give you a result based on being a light or dark image. – Shorn Mar 10 '23 at 07:35
  • Your approach does not make any sense, its just classifying the first imagenet class, not dark/light. – Dr. Snoopy Mar 10 '23 at 09:17

1 Answers1

0

I believe this is not the correct approach for this task.

MobileNetV2 is a model architecture that can be used for tasks such as image classification, object detection and image segmentation. When initializing it like this:

model = tf.keras.applications.MobileNetV2(weights='imagenet')

You are loading the model weights which were produced when training on the Imagenet dataset. This dataset is created to be used in training object recognition models.

Using OpenCV might be a better option for your use case. This article describes a similar scenario and perhaps this stackoverflow question is also relevant.

Bogdan
  • 118
  • 1
  • 3
  • 11