Untitled

mail@pastecode.io avatar
unknown
plain_text
12 days ago
2.3 kB
1
Indexable
Never
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import DepthwiseConv2D, Flatten, Dense
from PIL import Image, ImageOps
import numpy as np
from os import system, name
from ecapture import ecapture as ec

ec.capture(0,"your image","thing.jpg")



def clear():
    _= system('cls')

# Disable scientific notation for clarity
np.set_printoptions(suppress=True)

# Load the labels
with open("labels.txt", "r") as f:
    class_names = f.readlines()

# Replace this with the path to your image
image_path = "thing.jpg"

# Load and preprocess the image
image = Image.open(image_path).convert("RGB")
image = ImageOps.fit(image, (224, 224), Image.LANCZOS)
image_array = np.asarray(image)
normalized_image_array = (image_array.astype(np.float32) / 127.5) - 1
data = np.expand_dims(normalized_image_array, axis=0)

# Define the model structure
model = Sequential([
    DepthwiseConv2D(kernel_size=(3, 3), strides=(1, 1), padding='same', depth_multiplier=1, activation='relu', use_bias=False, input_shape=(224, 224, 3)),
    Flatten(),
    Dense(len(class_names), activation='softmax')
])

# Load weights into the model
try:
    model.load_weights("keras_Model.h5", by_name=True)
except ValueError as e:
    print("Error loading weights:", e)

prediction = model.predict(data)
index = np.argmax(prediction)
class_name = class_names[index].strip()
confidence_score = prediction[0][index]
dots = 0
while not confidence_score >= 0.9:
    if dots == 0:
        print("loading")
    if dots == 1:
        print("loading.")
    if dots == 2:
        print("loading..")
    if dots == 3:
        print("loading...")
        dots = -1
    model = Sequential([
        DepthwiseConv2D(kernel_size=(3, 3), strides=(1, 1), padding='same', depth_multiplier=1, activation='relu',
                        use_bias=False, input_shape=(224, 224, 3)),
        Flatten(),
        Dense(len(class_names), activation='softmax')
    ])
    prediction = model.predict(data)
    index = np.argmax(prediction)
    class_name = class_names[index].strip()
    confidence_score = prediction[0][index]

    dots = dots+1
    clear()
print("Confidence Score:", confidence_score)
print("Class:", class_name)
Leave a Comment