cvlib icon indicating copy to clipboard operation
cvlib copied to clipboard

Object Detection - How to only look for specific objects

Open sebastiankelly1234 opened this issue 6 years ago • 14 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?

sebastiankelly1234 avatar Oct 22 '18 11:10 sebastiankelly1234

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 avatar Oct 24 '18 11:10 arunponnusamy

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

bogdan245 avatar Jan 04 '19 10:01 bogdan245

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.

arunponnusamy avatar Jan 06 '19 09:01 arunponnusamy

im having an issue with tensorflow-gpu

ConceptCodes avatar Feb 08 '19 00:02 ConceptCodes

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.

jier avatar May 07 '19 20:05 jier

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.

jier avatar May 07 '19 20:05 jier

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)

arall avatar Jan 25 '20 16:01 arall

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 avatar Jan 27 '20 19:01 ArnoldLalika

@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.

arunponnusamy avatar Feb 06 '20 07:02 arunponnusamy

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 avatar Feb 06 '20 20:02 ArnoldLalika

@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?

Gujar-Shubham avatar Sep 14 '20 15:09 Gujar-Shubham

@Gujar-Shubham This has been implemented already. Have a look at the readme and examples folder. https://github.com/arunponnusamy/cvlib#custom-trained-yolo-weights https://github.com/arunponnusamy/cvlib/tree/master/examples

arunponnusamy avatar Sep 15 '20 01:09 arunponnusamy

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

rabihiawaludin avatar Nov 22 '20 21:11 rabihiawaludin

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.

Rudra7781 avatar Jan 22 '22 08:01 Rudra7781