philipperemy / yolo-9000

YOLO9000: Better, Faster, Stronger - Real-Time Object Detection. 9000 classes!
Apache License 2.0
1.18k stars 309 forks source link

Using live webcam #40

Open abdulrashid123 opened 4 years ago

abdulrashid123 commented 4 years ago

I need to use live Web cam for object detection and extract centered boxed image

srividya91 commented 4 years ago

@abdulrashid123 have you worked on object detection with yolo9000.? if so please tell me how you worked..

srividya91 commented 4 years ago

@philipperemy. I need to work on live object detection with yolo9000. I tried by passing images and video (mentioned in your page).but, i want to take llive feed..

please help me

philipperemy commented 4 years ago

@abdulrashid123

Edit the makefile and change OPENCV=1

If you are on linux:

sudo apt-get install libopencv-dev
./darknet detector demo cfg/combine9k.data cfg/yolo9000.cfg ../yolo9000-weights/yolo9000.weights -c 0

Should work hopefully.

philipperemy commented 4 years ago

If you want to use it with MacOSX (which I think is a bad idea because it will be down too slow, you can apply this patch in darknet):

Copy this file to /Users/premy/PycharmProjects/yolo-9000/darknet/patch. Then run:

/Users/premy/PycharmProjects/yolo-9000/darknet
git apply patch
brew install opencv3
make clean
make
./darknet detector demo cfg/combine9k.data cfg/yolo9000.cfg ../yolo9000-weights/yolo9000.weights -c 0
diff --git a/Makefile b/Makefile
index 63e15e6..3b4ac95 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 GPU=0
 CUDNN=0
-OPENCV=0
+OPENCV=1
 OPENMP=0
 DEBUG=0

@@ -42,8 +42,8 @@ CFLAGS+=$(OPTS)
 ifeq ($(OPENCV), 1)
 COMMON+= -DOPENCV
 CFLAGS+= -DOPENCV
-LDFLAGS+= `pkg-config --libs opencv` -lstdc++
-COMMON+= `pkg-config --cflags opencv`
+LDFLAGS+= `pkg-config --libs opencv4` -lstdc++
+COMMON+= `pkg-config --cflags opencv4`
 endif

 ifeq ($(GPU), 1)
@@ -83,7 +83,7 @@ $(SLIB): $(OBJS)
    $(CC) $(CFLAGS) -shared $^ -o $@ $(LDFLAGS)

 $(OBJDIR)%.o: %.cpp $(DEPS)
-   $(CPP) $(COMMON) $(CFLAGS) -c $< -o $@
+   $(CPP) $(COMMON) $(CFLAGS) -std=c++11 -c $< -o $@

 $(OBJDIR)%.o: %.c $(DEPS)
    $(CC) $(COMMON) $(CFLAGS) -c $< -o $@
diff --git a/src/image_opencv.cpp b/src/image_opencv.cpp
index 7511280..baab686 100644
--- a/src/image_opencv.cpp
+++ b/src/image_opencv.cpp
@@ -9,30 +9,36 @@ using namespace cv;

 extern "C" {

-IplImage *image_to_ipl(image im)
+Mat image_to_mat(image im)
 {
+    assert(im.c == 3 || im.c == 1);
     int x,y,c;
-    IplImage *disp = cvCreateImage(cvSize(im.w,im.h), IPL_DEPTH_8U, im.c);
-    int step = disp->widthStep;
+    image copy = copy_image(im);
+    constrain_image(copy);
+    if(im.c == 3) rgbgr_image(copy);
+    unsigned char *data = (unsigned char *)malloc(im.w * im.h * im.c);
     for(y = 0; y < im.h; ++y){
         for(x = 0; x < im.w; ++x){
             for(c= 0; c < im.c; ++c){
-                float val = im.data[c*im.h*im.w + y*im.w + x];
-                disp->imageData[y*step + x*im.c + c] = (unsigned char)(val*255);
+                float val = copy.data[c*im.h*im.w + y*im.w + x];
+                data[y*im.w*im.c + x*im.c + c] = (unsigned char)(val*255);
             }
         }
     }
-    return disp;
+    Mat m(im.h, im.w, CV_MAKETYPE(CV_8U, im.c), data);
+    free_image(copy);
+    free(data);
+    return m;
 }

-image ipl_to_image(IplImage* src)
+image mat_to_image(Mat m)
 {
-    int h = src->height;
-    int w = src->width;
-    int c = src->nChannels;
+    int h = m.rows;
+    int w = m.cols;
+    int c = m.channels();
     image im = make_image(w, h, c);
-    unsigned char *data = (unsigned char *)src->imageData;
-    int step = src->widthStep;
+    unsigned char *data = (unsigned char*)m.data;
+    int step = m.step;
     int i, j, k;

     for(i = 0; i < h; ++i){
@@ -42,26 +48,6 @@ image ipl_to_image(IplImage* src)
             }
         }
     }
-    return im;
-}
-
-Mat image_to_mat(image im)
-{
-    image copy = copy_image(im);
-    constrain_image(copy);
-    if(im.c == 3) rgbgr_image(copy);
-
-    IplImage *ipl = image_to_ipl(copy);
-    Mat m = cvarrToMat(ipl, true);
-    cvReleaseImage(&ipl);
-    free_image(copy);
-    return m;
-}
-
-image mat_to_image(Mat m)
-{
-    IplImage ipl = m;
-    image im = ipl_to_image(&ipl);
     rgbgr_image(im);
     return im;
 }
@@ -72,9 +58,9 @@ void *open_video_stream(const char *f, int c, int w, int h, int fps)
     if(f) cap = new VideoCapture(f);
     else cap = new VideoCapture(c);
     if(!cap->isOpened()) return 0;
-    if(w) cap->set(CV_CAP_PROP_FRAME_WIDTH, w);
-    if(h) cap->set(CV_CAP_PROP_FRAME_HEIGHT, w);
-    if(fps) cap->set(CV_CAP_PROP_FPS, w);
+    if(w) cap->set(CAP_PROP_FRAME_WIDTH, w);
+    if(h) cap->set(CAP_PROP_FRAME_HEIGHT, w);
+    if(fps) cap->set(CAP_PROP_FPS, w);
     return (void *) cap;
 }

@@ -123,7 +109,7 @@ void make_window(char *name, int w, int h, int fullscreen)
 {
     namedWindow(name, WINDOW_NORMAL);
     if (fullscreen) {
-        setWindowProperty(name, CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
+        setWindowProperty(name, WND_PROP_FULLSCREEN, WINDOW_FULLSCREEN);
     } else {
         resizeWindow(name, w, h);
         if(strcmp(name, "Demo") == 0) moveWindow(name, 0, 0);

Referring to :

image

Leela-Nayan commented 3 years ago

Getting the error "corrupt patch at line 134" Tried many ways to correct it. Pls resolve at the earliest @philipperemy

If you want to use it with MacOSX (which I think is a bad idea because it will be down too slow, you can apply this patch in darknet):

Copy this file to /Users/premy/PycharmProjects/yolo-9000/darknet/patch. Then run:

/Users/premy/PycharmProjects/yolo-9000/darknet
git apply patch
brew install opencv3
make clean
make
./darknet detector demo cfg/combine9k.data cfg/yolo9000.cfg ../yolo9000-weights/yolo9000.weights -c 0
diff --git a/Makefile b/Makefile
index 63e15e6..3b4ac95 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 GPU=0
 CUDNN=0
-OPENCV=0
+OPENCV=1
 OPENMP=0
 DEBUG=0

@@ -42,8 +42,8 @@ CFLAGS+=$(OPTS)
 ifeq ($(OPENCV), 1)
 COMMON+= -DOPENCV
 CFLAGS+= -DOPENCV
-LDFLAGS+= `pkg-config --libs opencv` -lstdc++
-COMMON+= `pkg-config --cflags opencv`
+LDFLAGS+= `pkg-config --libs opencv4` -lstdc++
+COMMON+= `pkg-config --cflags opencv4`
 endif

 ifeq ($(GPU), 1)
@@ -83,7 +83,7 @@ $(SLIB): $(OBJS)
  $(CC) $(CFLAGS) -shared $^ -o $@ $(LDFLAGS)

 $(OBJDIR)%.o: %.cpp $(DEPS)
- $(CPP) $(COMMON) $(CFLAGS) -c $< -o $@
+ $(CPP) $(COMMON) $(CFLAGS) -std=c++11 -c $< -o $@

 $(OBJDIR)%.o: %.c $(DEPS)
  $(CC) $(COMMON) $(CFLAGS) -c $< -o $@
diff --git a/src/image_opencv.cpp b/src/image_opencv.cpp
index 7511280..baab686 100644
--- a/src/image_opencv.cpp
+++ b/src/image_opencv.cpp
@@ -9,30 +9,36 @@ using namespace cv;

 extern "C" {

-IplImage *image_to_ipl(image im)
+Mat image_to_mat(image im)
 {
+    assert(im.c == 3 || im.c == 1);
     int x,y,c;
-    IplImage *disp = cvCreateImage(cvSize(im.w,im.h), IPL_DEPTH_8U, im.c);
-    int step = disp->widthStep;
+    image copy = copy_image(im);
+    constrain_image(copy);
+    if(im.c == 3) rgbgr_image(copy);
+    unsigned char *data = (unsigned char *)malloc(im.w * im.h * im.c);
     for(y = 0; y < im.h; ++y){
         for(x = 0; x < im.w; ++x){
             for(c= 0; c < im.c; ++c){
-                float val = im.data[c*im.h*im.w + y*im.w + x];
-                disp->imageData[y*step + x*im.c + c] = (unsigned char)(val*255);
+                float val = copy.data[c*im.h*im.w + y*im.w + x];
+                data[y*im.w*im.c + x*im.c + c] = (unsigned char)(val*255);
             }
         }
     }
-    return disp;
+    Mat m(im.h, im.w, CV_MAKETYPE(CV_8U, im.c), data);
+    free_image(copy);
+    free(data);
+    return m;
 }

-image ipl_to_image(IplImage* src)
+image mat_to_image(Mat m)
 {
-    int h = src->height;
-    int w = src->width;
-    int c = src->nChannels;
+    int h = m.rows;
+    int w = m.cols;
+    int c = m.channels();
     image im = make_image(w, h, c);
-    unsigned char *data = (unsigned char *)src->imageData;
-    int step = src->widthStep;
+    unsigned char *data = (unsigned char*)m.data;
+    int step = m.step;
     int i, j, k;

     for(i = 0; i < h; ++i){
@@ -42,26 +48,6 @@ image ipl_to_image(IplImage* src)
             }
         }
     }
-    return im;
-}
-
-Mat image_to_mat(image im)
-{
-    image copy = copy_image(im);
-    constrain_image(copy);
-    if(im.c == 3) rgbgr_image(copy);
-
-    IplImage *ipl = image_to_ipl(copy);
-    Mat m = cvarrToMat(ipl, true);
-    cvReleaseImage(&ipl);
-    free_image(copy);
-    return m;
-}
-
-image mat_to_image(Mat m)
-{
-    IplImage ipl = m;
-    image im = ipl_to_image(&ipl);
     rgbgr_image(im);
     return im;
 }
@@ -72,9 +58,9 @@ void *open_video_stream(const char *f, int c, int w, int h, int fps)
     if(f) cap = new VideoCapture(f);
     else cap = new VideoCapture(c);
     if(!cap->isOpened()) return 0;
-    if(w) cap->set(CV_CAP_PROP_FRAME_WIDTH, w);
-    if(h) cap->set(CV_CAP_PROP_FRAME_HEIGHT, w);
-    if(fps) cap->set(CV_CAP_PROP_FPS, w);
+    if(w) cap->set(CAP_PROP_FRAME_WIDTH, w);
+    if(h) cap->set(CAP_PROP_FRAME_HEIGHT, w);
+    if(fps) cap->set(CAP_PROP_FPS, w);
     return (void *) cap;
 }

@@ -123,7 +109,7 @@ void make_window(char *name, int w, int h, int fullscreen)
 {
     namedWindow(name, WINDOW_NORMAL);
     if (fullscreen) {
-        setWindowProperty(name, CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
+        setWindowProperty(name, WND_PROP_FULLSCREEN, WINDOW_FULLSCREEN);
     } else {
         resizeWindow(name, w, h);
         if(strcmp(name, "Demo") == 0) moveWindow(name, 0, 0);

Referring to :

image

@philipperemy

philipperemy commented 3 years ago

@Leela-Nayan it's not a good idea to use it on a mac without GPU support as it will be incredibly slow.

Shashi630 commented 8 months ago

@philipperemy ./darknet detector demo cfg/combine9k.data cfg/yolo9000.cfg ../yolo9000-weights/yolo9000.weights -c 0 Demo layer filters size input output 0 conv 32 3 x 3 / 1 544 x 544 x 3 -> 544 x 544 x 32 0.511 BFLOPs 1 max 2 x 2 / 2 544 x 544 x 32 -> 272 x 272 x 32 2 conv 64 3 x 3 / 1 272 x 272 x 32 -> 272 x 272 x 64 2.727 BFLOPs 3 max 2 x 2 / 2 272 x 272 x 64 -> 136 x 136 x 64 4 conv 128 3 x 3 / 1 136 x 136 x 64 -> 136 x 136 x 128 2.727 BFLOPs 5 conv 64 1 x 1 / 1 136 x 136 x 128 -> 136 x 136 x 64 0.303 BFLOPs 6 conv 128 3 x 3 / 1 136 x 136 x 64 -> 136 x 136 x 128 2.727 BFLOPs 7 max 2 x 2 / 2 136 x 136 x 128 -> 68 x 68 x 128 8 conv 256 3 x 3 / 1 68 x 68 x 128 -> 68 x 68 x 256 2.727 BFLOPs 9 conv 128 1 x 1 / 1 68 x 68 x 256 -> 68 x 68 x 128 0.303 BFLOPs 10 conv 256 3 x 3 / 1 68 x 68 x 128 -> 68 x 68 x 256 2.727 BFLOPs 11 max 2 x 2 / 2 68 x 68 x 256 -> 34 x 34 x 256 12 conv 512 3 x 3 / 1 34 x 34 x 256 -> 34 x 34 x 512 2.727 BFLOPs 13 conv 256 1 x 1 / 1 34 x 34 x 512 -> 34 x 34 x 256 0.303 BFLOPs 14 conv 512 3 x 3 / 1 34 x 34 x 256 -> 34 x 34 x 512 2.727 BFLOPs 15 conv 256 1 x 1 / 1 34 x 34 x 512 -> 34 x 34 x 256 0.303 BFLOPs 16 conv 512 3 x 3 / 1 34 x 34 x 256 -> 34 x 34 x 512 2.727 BFLOPs 17 max 2 x 2 / 2 34 x 34 x 512 -> 17 x 17 x 512 18 conv 1024 3 x 3 / 1 17 x 17 x 512 -> 17 x 17 x1024 2.727 BFLOPs 19 conv 512 1 x 1 / 1 17 x 17 x1024 -> 17 x 17 x 512 0.303 BFLOPs 20 conv 1024 3 x 3 / 1 17 x 17 x 512 -> 17 x 17 x1024 2.727 BFLOPs 21 conv 512 1 x 1 / 1 17 x 17 x1024 -> 17 x 17 x 512 0.303 BFLOPs 22 conv 1024 3 x 3 / 1 17 x 17 x 512 -> 17 x 17 x1024 2.727 BFLOPs 23 conv 28269 1 x 1 / 1 17 x 17 x1024 -> 17 x 17 x28269 16.732 BFLOPs 24 detection mask_scale: Using default '1.000000' Loading weights from ../yolo9000-weights/yolo9000.weights...Done! [ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (1758) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module v4l2src0 reported: Cannot identify device '/dev/video0'. [ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (888) open OpenCV | GStreamer warning: unable to start pipeline [ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (480) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created [ WARN:0] global ../modules/videoio/src/cap_v4l.cpp (887) open VIDEOIO(V4L2:/dev/video0): can't open camera by index Couldn't connect to webcam. : Resource temporarily unavailable darknet: ./src/utils.c:256: error: Assertion `0' failed. Aborted

how to solve this error.