Issue
Okay, so, I am currently using this code:
import cv2
from PIL import Image, ImageOps
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import load_model
classesint = []
classesname = []
file = open("labels.txt", "r")
for line in file:
stripped_line = line.strip()
line_list = stripped_line.split()
classesint.append(int(line_list[0]))
line_list.remove(line_list[0])
classesname.append(" ".join(line_list))
print(classesint)
print(classesname)
file.close()
model = load_model('keras_model.h5')
vid = cv2.VideoCapture(0, cv2.CAP_DSHOW)
while True:
ret, frame = vid.read()
if not ret:
pass
else:
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
image = Image.fromarray(frame)
size = (224, 224)
image = ImageOps.fit(image, size, Image.ANTIALIAS)
image_array = np.asarray(image)
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
data[0] = normalized_image_array
prediction = model.predict(data)
clas = np.argmax(prediction, axis = 1)
threshold = 97
detected = False
for i in classesint:
if prediction[0][i]*100 >= threshold:
detected = True
if detected:
# for i in classesint:
# print(prediction[0][i]*100)
print(classesname[int(clas)])
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
vid.release()
cv2.destroyAllWindows()
to have real time webcam object detection, and it works fine, but I was wondering how I could see WHERE it detects this object? Would I need to rewrite my entire file? Or is there a few liner I could add that could do this?
Solution
It looks like you are using a classification model, which outputs which type of image you inputted. If you use a object detection model, such as YOLO you can find the location as well, it all depends on how you trained the model.
Classification models are trained with class labels, and detection models are trained with class labels + object position. Hence they produce the outputs they are trained for.
Answered By - Olca Orakcı
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.