arunponnusamy / cvlib

A simple, high level, easy to use, open source Computer Vision library for Python.

Home Page:http://arunponnusamy.com/cvlib/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Object Detection - How to only look for specific objects

sebastiankelly1234 opened this issue · comments

I am using the object detection script, which searches for ~80 objects in the image. Is it possible to refine this to only search for selected objects?

Hello @sebastiankelly1234 , the pre-trained model is trained on 80 object classes. I am not sure if we can restrict it to look for specific objects. We can choose not to process the detections of the objects that we are not interested in further (not drawing rectangle, writing text over the box etc). If we want to search for only one object strictly, we need to train the model with only that particular object class.

@arunponnusamy How can I restrict the program from drawing the rectangle and writing text over objects i dont want to detect?

Hello @bogdan245 , if you look at the example https://github.com/arunponnusamy/cvlib/blob/eea8647495b40458bc6877ac67898ed8a3df0abf/examples/object_detection.py
out = draw_bbox(image, bbox, label, conf)
this is the line of code which actually draws the bounding box over detected objects in the image. You can remove the objects which you are not interested in. For example, let's say detected objects are car,bicycle and bottle. But you are interested only in car. The label list will look something like ['car', 'bicycle', 'bottle']. bbox and conf will have the corresponding coordinates and confidence values.

So you can loop through the items in label list and remove the objects which you are not interested in from the list and corresponding values in bbox and conf before calling draw_bbox( ).

Hope this helps.

im having an issue with tensorflow-gpu

Hi, First I am very grateful for this library you have put the effort to create. I just have a question about object-detection webcam tiny-yolo you have made. It crashes every time if it just recognise on or two objects. The error originates in draw_box line number 52. Some examples

['bottle', 'bottle'] Traceback (most recent call last): File "object_detection_tiny_yolo.py", line 37, in <module> out = draw_bbox(frame, bbox, label, conf, write_conf=True) File "/home/jier/.local/lib/python2.7/site-packages/cvlib/object_detection.py", line 52, in draw_bbox cv2.rectangle(img, (bbox[i][0],bbox[i][1]), (bbox[i][2],bbox[i][3]), color, 2) TypeError: only length-1 arrays can be converted to Python scalars

['bottle', 'bottle', 'bottle', 'person', 'clock'] Traceback (most recent call last): File "object_detection_tiny_yolo.py", line 37, in <module> out = draw_bbox(frame, bbox, label, conf, write_conf=True) File "/home/jier/.local/lib/python2.7/site-packages/cvlib/object_detection.py", line 52, in draw_bbox cv2.rectangle(img, (bbox[i][0],bbox[i][1]), (bbox[i][2],bbox[i][3]), color, 2) TypeError: only length-1 arrays can be converted to Python scalars.
I have not cloned you repo yet, I was just curious, but for all the other examples gender, face_detection worked smoothly., only object detection is not working properly

I am using python 2 because I have to, no that I want to. If anyone has come across the same problem please share if you'd like. Thank you.

Hi, First I am very grateful for this library you have put the effort to create. I just have a question about object-detection webcam tiny-yolo you have made. It crashes every time if it just recognise on or two objects. The error originates in draw_box line number 52. Some examples

['bottle', 'bottle'] Traceback (most recent call last): File "object_detection_tiny_yolo.py", line 37, in <module> out = draw_bbox(frame, bbox, label, conf, write_conf=True) File "/home/jier/.local/lib/python2.7/site-packages/cvlib/object_detection.py", line 52, in draw_bbox cv2.rectangle(img, (bbox[i][0],bbox[i][1]), (bbox[i][2],bbox[i][3]), color, 2) TypeError: only length-1 arrays can be converted to Python scalars

['bottle', 'bottle', 'bottle', 'person', 'clock'] Traceback (most recent call last): File "object_detection_tiny_yolo.py", line 37, in <module> out = draw_bbox(frame, bbox, label, conf, write_conf=True) File "/home/jier/.local/lib/python2.7/site-packages/cvlib/object_detection.py", line 52, in draw_bbox cv2.rectangle(img, (bbox[i][0],bbox[i][1]), (bbox[i][2],bbox[i][3]), color, 2) TypeError: only length-1 arrays can be converted to Python scalars.
I have not cloned you repo yet, I was just curious, but for all the other examples gender, face_detection worked smoothly., only object detection is not working properly

I am using python 2 because I have to, no that I want to. If anyone has come across the same problem please share if you'd like. Thank you.

I figured it out, the color variable is a numpy array from
COLORS = np.random.uniform(0, 255, size=(80, 3)) color = COLORS[classes.index(label)]
cv2.rectangle expect a one length array as the error suggest. So I rewrote the draw boxes myself without the different colors.

Would be something like:

cv2.rectangle(img, (int(bbox[i][0]), int(bbox[i][1])), (int(bbox[i][2]), int(bbox[i][3])), (0, 200, 0), 2)

Hello. Thank you I got this code from your library, which is already trained to detect cars and it works perfectly, but my concern is to use this code to detect crosswalks instead of cars, so which line of this code is dealing with the trained datasets? so as I can train my own dataset and is it possible?

import cv2
import matplotlib.pyplot as plt
import cvlib as cv
from cvlib.object_detection import draw_bbox

im = cv2.imread(r"C:\Users\n01******\Desktop\crosswalk.jpg")

bbox, label, conf = cv.detect_common_objects(im)

output_image = draw_bbox(im, bbox, label, conf)

plt.imshow(output_image)
plt.show()

print('Number of cars in the image is '+ str(label.count('car')))

@ArnoldLalika I am currently working on adding a method to use custom trained weights instead of pretrained weights. Will be adding this in the next release. Meanwhile have a look at this blog post.

I'm glad to hear that from you. I'm just waiting for the next release, when should I expect it? Thanks in advance.

@ArnoldLalika I am currently working on adding a method to use custom trained weights instead of pretrained weights. Will be adding this in the next release. Meanwhile have a look at this blog post.

I have custom trained yolo weights, cfg file, txt file How can I implement this?

Hi, I want to detect fish. But no data in common objects. How to define new data like "fish"? Thank you

commented

for this code


:

import cv2
import matplotlib.pyplot as plt
import cvlib as cv
from cvlib.object_detection import draw_bbox

im = cv2.imread("./cars_4.jpeg")

bbox, label, conf = cv.detect_common_objects(im)

output_image = draw_bbox(im, bbox, label, conf)

plt.imshow(output_image)
plt.show()

print('Number of cars in the image is '+ str(label.count('car')))

this is the error that i got:


Traceback (most recent call last):
File "/Users/sound/Downloads/Car detection 101/test.py", line 8, in
bbox, label, conf = cv.detect_common_objects(im)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/cvlib/object_detection.py", line 135, in detect_common_objects
outs = net.forward(get_output_layers(net))
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/cvlib/object_detection.py", line 29, in get_output_layers
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/cvlib/object_detection.py", line 29, in
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
IndexError: invalid index to scalar variable.