YOLO (Darknet): How to detect a whole directory of images? - machine-learning

The Darknet guide to detect objects in images using pre-trained weights is here: https://pjreddie.com/darknet/yolo/
The command to run is:
./darknet detect cfg/yolov3.cfg yolov3.weights data/dog.jpg
The last argument is the path to a file, I've tried to change it to data/*.jpg but didn't work.
How to use Darknet to detect a whole directory of images?

As per the link mentioned below, one can use cv2.dnn.readNetFromDarknet module to read darknet, trained weights and configuration file to make a loaded model in python. Once the model is loaded, one can simply use for loop for prediction.
Please refer this link for further clarification

There is a simple way to detect objects on a list of images based on this repository AlexeyAB/darknet.
./darknet detector test cfg/obj.data cfg/yolov3.cfg yolov3.weights < images_files.txt
You can generate the file list either from the command line (Send folder files to txt ) or using a GUI tool like Nautilus on Ubuntu.
Two extra flags -dont_show -save_labels will disable the user interaction, and save the detection results to text files instead.

There's a trick to make Darknet executable load weights once and infer multiple image files. Use expect to do the trick.
Install expect:
sudo yum install expect -y
#sudo apt install expect -y
Do object detection on multiple images:
expect <<"HEREDOC"
puts "Spawning...";
spawn ./darknet detect cfg/yolov3-tiny.cfg yolov3-tiny.weights;
set I 0;
expect {
"Enter Image Path" {
set timeout -1;
if {$I == 0} {
send "data/dog.jpg\r";
incr I;
} elseif {$I == 1} {
send "data/kite.jpg\r";
incr I;
} else {
exit;
}
exp_continue;
}
}
HEREDOC

Another solution is loading Darknet from Python2 (not 3, Darknet is using Python2).
1a) Clone darknet as described in https://pjreddie.com/darknet/yolo/
1b) Go to the cloned dir, download yolov3-tiny.weights and yolov3.weights as said in https://pjreddie.com/darknet/yolo/
2) Copy darknet/examples/detector.py to darknet dir
3) Edit the new detector.py
Change .load_net line to use: cfg/yolov3-tiny.cfg and yolov3-tiny.weights
Change .load_meta line to use: cfg/coco.data
4a) Detect objects in images by adding some dn.dectect lines in detector.py
4b) Run detector.py

Related

pytorch torchvision.datasets.ImageFolder FileNotFoundError: Found no valid file for the classes .ipynb_checkpoints

Tried to load training data with pytorch torch.datasets.ImageFolder in Colab.
transform = transforms.Compose([transforms.Resize(400),
transforms.ToTensor()])
dataset_path = 'ss/'
dataset = datasets.ImageFolder(root=dataset_path, transform=transform)
dataloader = torch.utils.data.DataLoader(dataset, batch_size=20)
I encountered the following error :
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-27-7abcc1f434b1> in <module>()
2 transforms.ToTensor()])
3 dataset_path = 'ss/'
----> 4 dataset = datasets.ImageFolder(root=dataset_path, transform=transform)
5 dataloader = torch.utils.data.DataLoader(dataset, batch_size=20)
3 frames
/usr/local/lib/python3.7/dist-packages/torchvision/datasets/folder.py in make_dataset(directory, class_to_idx, extensions, is_valid_file)
100 if extensions is not None:
101 msg += f"Supported extensions are: {', '.join(extensions)}"
--> 102 raise FileNotFoundError(msg)
103
104 return instances
FileNotFoundError: Found no valid file for the classes .ipynb_checkpoints. Supported extensions are: .jpg, .jpeg, .png, .ppm, .bmp, .pgm, .tif, .tiff, .webp
My Dataset folder contains a subfolder with many training images in png format, still the ImageFolder can't access them.
I encountered the same problem when I was using IPython notebook-like tools.
First please check if there is any hidden files under your dataset_path. Use ls -a if you are under a Linux environment.
The case happen to me is I found a hidden file called .ipynb_checkpoints which is located parallelly to image class subfolders. I think that file causes confusion to PyTorch dataset. I made sure it is not useful so I simply deleted it. Then the dataset works fine.
Or if you would like to simply ignore that file, you may also try this.
The files in the image folder need to be placed in the subfolders for each class, like this:
root/dog/xxx.png
root/dog/xxy.png
root/dog/[...]/xxz.png
root/cat/123.png
root/cat/nsdf3.png
root/cat/[...]/asd932_.png
https://pytorch.org/vision/stable/datasets.html#torchvision.datasets.ImageFolder
Are your files in ss dir organized in this way?
1- The files in the image folder need to be placed in the subfolders for each class (as said Sergii Dymchenko)
2- Put the absolute path when using google colab
The solution for google colaboratory:
When you creating a directory, coollaboratory additionally creates .ipynb_checkpoints in it.
To solve the problem, it is enough to remove it from the folder containing directories with images (i.e. from the train folder). You need to run:
!rm -R test/train/.ipynb_checkpoints
!ls test/train/ -a #to make sure that the deletion has occurred
where test/train/ is my path to datasets folders

cv2.imshow() function is opening a window that always says not responding - python opencv

I am trying to run a very simple program. To open and jpg file and display it using the opencv library for python. Initially it all worked fine but now it just opens a window which doesn't show the image but says 'not responding'. I need to go to the task manager and close it!
from numpy import *
import matplotlib as plt
import cv2
img = cv2.imread('amandapeet.jpg')
print img.shape
cv2.imshow('Amanda', img)
You missed one more line:
cv2.waitKey(0)
Then the window shows the image until you press any key on keyboard. Or you can pass as following:
cv2.waitKey(1000)
cv2.destroyAllWindows()
Here, window shows image for 1000 ms, or 1 second. After that, the window would disappear itself. But in some cases, it won't. So you can forcefully destroy it using cv2.destroyAllWindows()
Please read more tutorials first : http://docs.opencv.org/trunk/doc/py_tutorials/py_tutorials.html
None of the answers here worked in MacOS. The following works:
Just add a cv2.waitKey(1) after cv2.destroyAllWindows().
Example:
import cv2
image = cv2.imread('my_image.jpg')
cv2.imshow('HSV image', hsv_image); cv2.waitKey(0); cv2.destroyAllWindows(); cv2.waitKey(1)
The solution that worked for me:
Switch from inline graphics to auto. It worked both in Spyder and in Jupyter notebooks.
To change Spyder setting:
Go to Tools > Preferences > IPhyton console > Graphics > Backend: Automatic
(Change backend from Inline to Automatic)
To change Notebook setting:
Enter command:
%matplotlib auto
Some background for my case (for those who may be quick to judge):
It used to work fine: I could open an image, it would load, and it would be responsive (doesn't say "Not responding", can close, focus, etc.) Then I installed some packages and ran some demo notebooks that apparently messed up some settings (Spyder open files were reset too).
I tried adding waitKey(1) (and 0, 30, 1000, etc values too). It made the image load, at least. But the image frame was "Not Responding": didn't refresh, couldn't close, didn't come to top, etc. Had to close using cv2.destroyAllWindows().
Note that everything worked fine during the duration of waitKey. I put this in a loop that shows the same image in the same named window and waits for a few seconds. During the loop everything works fine. As soon as the loop ends, the image window is "Not responding" (which looks like a GUI thread issue). I tried using cv2.startWindowThread(), and didn't make any difference.
Finally, changing from Inline graphics to Auto brought everything back to order.
I've been working with opencv 3.2 and matplotlib too recently and discovered (through trial and error of commenting out lines) that the import of pyplot from matplotlib has some sort of interference with the cv2.imshow() function. I'm not sure why or how it really works but in case anyone searches for this issue and comes across this old forum, this might help. I'm working to try to find a solution around this interference bu
I did also face the same issue. I am running through command line python prompt in centos 7 with the following code
>> import cv2, numpy as np
>> cap=cv2.VideoCapture(0)
>> img=cap.read()
>> cap.release()
>> cv2.imshow('image',img[1])
>> cv2.waitKey(0)
>> cv2.destroyAllWindows()
>> cv2.waitKey(1)
Even then the problem persisted and didn't solve. So I added
>> cv2.imshow('image',img[1])
Adding this did close the image window.Running the command again would create a new instance. Hope you can try if you still face any issues.
The cv2.imshow() function always takes two more functions to load and close the image. These two functions are cv2.waitKey() and cv2.destroyAllWindows(). Inside the cv2.waitKey() function, you can provide any value to close the image and continue with further lines of code.
# First line will provide resizing ability to the window
cv.namedWindow('Amanda', cv.WINDOW_AUTOSIZE)
# Show the image, note that the name of the output window must be same
cv.imshow('Amanda', img)
# T0 load and hold the image
cv.waitKey(0)
# To close the window after the required kill value was provided
cv.destroyAllWindows()
Hoping that you will get the image in a separate window now.
I've installed opencv-contrib-python library instead of opencv-python and now cv2.imshow() function works as expected.
If you have used python notebooks then there is a problem in using cv2.waitKey(0) and cv2.destroyallwindows() in Unix based system to run a program of opencv.
I have an alternative method which would prevent from freezing your image
Steps: -Copy the code from python notebooks and create new filename.py and paste it
- Open terminal
- cd path/to/file
- source activate VirtualEnvironment
- python filename.py
This will run code directly from terminal. Hope this helps you. Example Link: https://youtu.be/8O-FW4Wm10s
I was having this same error until I added the below lines of code. For the waitKey, you can input figures above 0(i.e 1, 100 and above). It serves as the delay time for the window and it is in milliseconds.
----> cv2 waitKey(0)
----> cv2 destroyAllWindows()
I found that i had a breakpoint on the
cv2.waitkey()
funtion. removing that fixed the issue for me
As I tried all solutions mentioned above, it works for displaying an image but in my case, I want to display the video not just the single image in the window, So to solve the problem added
k=cv2.waitkey(10)
if k == 27:
break
after cv2.imshow('title',img)

How to manipulate components of pathnames in Tcl?

I'm writing a reporting script that's part of a portable testing package. The user can unzip the package anywhere on their system, which is fine, but it means I can't hardcode a path.
Let's say that on my system, this script lives at C:/projects/testpackage/foo/bar/script.tcl. I need to set a variable, packageLocation, the path to /testpackage. In this example, it would be C:/Projects/testpackage. But when the user gets the package, he or she could put it anywhere, like so:
C:/Users/whatever/testpackage.
So, how can I call two levels up from the location of my currently running script? In Batch, I could do
:: example.bat
cd %~dp0
cd ../..
set packageLocation=%cd%
In Tcl, I'm lost. I know that the current location of the running script can be called as $::argv0. I've tried using cd ../.., but to no avail. It tries to set packageLocation as "../..C:/Projects/testpackage/foo/bar/script.tcl."
Any help is appreciated. Thanks!
set where [file normalize [file dirname [info script]]]
set parts [file split $where]
set pkgloc [file join {*}[lrange $parts 0 end-2]]
Should do what you want.
It goes like this:
Obtains the directory name of the file from which the current script was read to be evaluated, then normalizes it (replaces ~, .. etc).
Splits obtained full pathname at path separators producing a list of path components.
Extracts a new list from the list of path components containing all them from the beginning except the last two, then joins them back to produce the final name.
If you have Tcl < 8.5, the last line will have to be rewritten:
set last [expr {[llength $parts] - 3}]
set pkgloc [eval [list file join] [lrange $parts 0 $last]]

Implementing bag of word algorithm from opencv sample codes

I am trying to implement bagofwords_classification.cpp from opencv version 2.4.5 sample codes.cpp . What are the changes that we are required to make in this .cpp file for proper working of code. I am new to opencv and still trying sample codes.
How and where to add the Feature detector,descriptor extractor, descriptor matcher ?? in that .cpp code
Whenever i debug any code it never display results but just output the info about what that .cpp file is gonna do. In (EXAMPLE) matching_to_many_images.cpp even the images are saved in the file but still no results are shown.
To show an image, you can use cvShowImage("Title",image) or imshow(). This depends on wheter to image is an IplImage or Mat.
The code example is not 'false', the program uses commandline arguments, thus to start it you need to add certain commands.
From the code
[feature detector]
Feature detector name (e.g. SURF, FAST...) - see createFeatureDetector() function.
[descriptor extractor]
Descriptor extractor name (e.g. SURF, SIFT) - see createDescriptorExtractor() function.
[descriptor matcher]
Descriptor matcher name (e.g. BruteForce) - see createDescriptorMatcher() function.
then from those arguments it calls
Ptr<FeatureDetector> featureDetector = createFeatureDetector( ddmParams.detectorType );
Ptr<DescriptorExtractor> descExtractor = createDescriptorExtractor( ddmParams.descriptorType );

Problems converting video to pictures using python 2.7

Tried a lot of options and am running out of ideas. I was hoping someone here could help. I am trying to write some code in python that will extract frames (say every tenth frame) from a video (.avi or .wmv) and create a picture (.jpg preferably - but other formats will do). I have had no success and was wondering if someone could assist me in solving my problem by providing an alternative to what I have tried and failed.
I have tried PyMedia (the example in their tutorial http://pymedia.org/tut/src/dump_video.py.html does not work - the program bombs out when it looks for the video codecs):
#dm= muxer.Demuxer( inFile.split( '.' )[ -1 ] ) This line does not work
dm= muxer.Demuxer( 'avi' ) #This modified line does seem to work however
i= 1
inFile = "VideoTest.avi"
f= open( inFile, 'rb' )
s= f.read( 400000 )
r= dm.parse( s )
v= filter( lambda x: x[ 'type' ]== muxer.CODEC_TYPE_VIDEO, dm.streams )
v_id= v[ 0 ][ 'index' ]
print 'Assume video stream at %d index: ' % v_id
c= vcodec.Decoder( dm.streams[ v_id ] ) #this is the point where it crashes.
I have tried OpenCV v2.2 for Python but that doesn't work either (I can get most of OpenCV to work - except the one function, CaptureFromFile, that I need does not work). I believe the reason this function doesn't work is because on Windows it requires highGui to operate and for some reason, python and opencv cannot find highgui eventhough it is in the correct directory. I also understand OpenCV has issues with finding and applying correct video codecs so I am not sure which is the cause of my problem.
I have looked at pyFFMPEG but the last build for that was version 2.6 and I am running python 2.7.
I am running this on Windows Vista and Windows 7 machines, have Python 2.7 and OpenCV 2.2 loaded on C:\ and all other python packages (pygames, pymedia, numpy, scipy) installed in C:\python27\Libs\site-packages..." I downloaded and installed from executables, all packages were built for python 2.7. My Path variable includes Python27 and OpenCV and I have a PYTHONPATH variable.
Thanks for any ideas or recommendations.
I tried the following to extract each frames as a separate image:
import cv2
file_path = "some\path\to\the\file.avi"
video_object = cv2.VideoCapture(path)
success = True
while success:
success,frame = video_object.read()
if success:
cv2.imwrite(path[:-4]+"_"+str(i)+".jpg",frame)
print("Done!")
This script saves each frame as an image in the same folder as the source file. It may not be the most efficient way to do this, but it works for me! I know my string parsing is not the most recommended, but it works.
video_object.read() returns two object. The first is a bool indicating whether the reading operation was a success or not, and the second is the image.
There may be limitations with respect to the video codec, of which I am not aware. I'm using Python 2.7 with the most recent version of openCV and Numpy as of 11/7/2013.

Resources