Giter VIP home page Giter VIP logo

object_detection_demo_live's Introduction

Object_Detection_demo_LIVE

This is the code for the "How to do Object Detection with OpenCV" live session by Siraj Raval on Youtube

##Overview

This is the code for this video on Youtube by Siraj Raval. We'll use OpenCV to detect a strawberry in an image. We'll perform a series of operations which i've documented in the code to eventually highlight the biggest strawberry in an image and then draw a green circle around it.

##Dependencies

  • openCV
  • matplotlib
  • numpy
  • math

You can use pip to install any missing dependencies. And you can install OpenCV using this guide.

##Usage

Run python demo.py to create a new image with the detected strawberry. The last 3 lines at the bottom of demo.py let you define the input image name and the output image name. This detection takes a split second. Deep learning would be more accurate but requires more computation currently. Sometimes you just need to quickly detect an image and don't mind handcrafted which features to look for.

##Credits

Credits for this code go to alexlouden i've merely created a wrapper to get people started.

object_detection_demo_live's People

Contributors

llsourcell avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

object_detection_demo_live's Issues

Call to CUDA function failed. SOS

terminate called after throwing an instance of 'std::runtime_error'
what(): NCCL error in: /opt/conda/conda-bld/pytorch_1614378063927/work/torch/lib/c10d/../c10d/NCCLUtils.hpp:155, unhandled cuda error, NCCL version 2.7.8
ncclUnhandledCudaError: Call to CUDA function failed.

RGB values for params of inRange

I don't really understand what's going into inRange from this:-

# Filter by colour
# 0-10 hue
#minimum red amount, max red amount
min_red = np.array([0, 30, 30])
max_red = np.array([10, 100, 100])
#layer
mask1 = cv2.inRange(image_blur_hsv, min_red, max_red)

#birghtness of a color is hue
# 170-180 hue
min_red2 = np.array([170, 30, 30])
max_red2 = np.array([180, 100, 100])
mask2 = cv2.inRange(image_blur_hsv, min_red2, max_red2)

I couldn't really understand from the docs.

What exactly is going in?

cv2.contourArea(cnt)

This is my code
import cv2
import numpy as np
img = cv2.imread('index.jpeg',0)
ret,thresh = cv2.threshold(img,127,255,0)
_,contours,hierarchy= cv2.findContours(thresh, 1, 2)

cnt = contour[0]
M = cv2.moments(cnt)
print(M)
area = cv2.contourArea(cnt)
perimeter = cv2.arcLength(cnt,True)

I'm getting this error. some one pls help me resolving it.
error: /tmp/build/80754af9/opencv_1512491964794/work/modules/imgproc/src/shapedescr.cpp:319: error: (-215) npoints >= 0 && (depth == 5 || depth == 4) in function contourArea

(-215:Assertion failed) VScn::contains(scn) && VDcn::contains(dcn) && VDepth::contains(depth) in function 'CvtHelper'

Traceback (most recent call last):
File "demo.py", line 132, in
result = find_strawberry(image)
File "demo.py", line 63, in find_strawberry
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
cv2.error: OpenCV(3.4.2) /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/color.hpp:253: error: (-215:Assertion failed) VScn::contains(scn) && VDcn::contains(dcn) && VDepth::contains(depth) in function 'CvtHelper'

Run fails on Arch Linux

First issue is this line:
contours, hierarchy = cv2.findContours(image, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

This says too many values to unpack. Also changed the name of the image from yo to berry.

So I added one more variable and getting this error:

OpenCV Error: Assertion failed (npoints >= 0 && (depth == CV_32F || depth == CV_32S)) in contourArea, file /build/opencv/src/opencv-3.1.0/modules/imgproc/src/shapedescr.cpp, line 314
Traceback (most recent call last):
  File "demo.py", line 132, in <module>
    result = find_strawberry(image)
  File "demo.py", line 113, in find_strawberry
    big_strawberry_contour, mask_strawberries = find_biggest_contour(mask_clean)
  File "demo.py", line 38, in find_biggest_contour
    contour_sizes = [(cv2.contourArea(contour), contour) for contour in contours]
  File "demo.py", line 38, in <listcomp>
    contour_sizes = [(cv2.contourArea(contour), contour) for contour in contours]
cv2.error: /build/opencv/src/opencv-3.1.0/modules/imgproc/src/shapedescr.cpp:314: error: (-215) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function contourArea

May be it is due to the OpenCV version I'm using, but I am not sure.

Even I had the same problem, and the solution was quiet easy. Remember 1 thing, if the RGB values of your image lie in the range of 0-255, make sure the values are not of data type 'float'. As OpenCV considers float only when values range from 0-1. If it finds a float value larger than 1 it clips off the value thinking floats only exsist between 0-1. Hence such errors generated. So convert the data type to uint8 if values are from 0-255.

Even I had the same problem, and the solution was quiet easy. Remember 1 thing, if the RGB values of your image lie in the range of 0-255, make sure the values are not of data type 'float'. As OpenCV considers float only when values range from 0-1. If it finds a float value larger than 1 it clips off the value thinking floats only exsist between 0-1. Hence such errors generated. So convert the data type to uint8 if values are from 0-255.
image = image.astype('uint8')
Check this Kaggle Kernal

Originally posted by @shravankumar9892 in #6 (comment)

thxx My issue got solved by this method

area=cv2.contourArea(contour) Error

import cv2
import numpy as np
import pyfirmata
cap = cv2.VideoCapture(0)
port = pyfirmata.Arduino('COM6')
port.digital[13].write(0)
port.digital[12].write(0)
port.digital[11].write(0)




while True:
    _,frame = cap.read()
    hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    
    #kırmızı
    low_red = np.array([161, 100, 100])
    high_red `=` np.array([170, 255, 255])
    mask_red = cv2.inRange(hsv_frame, low_red, high_red)
    #yeşil
    low_green = np.array([38, 100, 100])
    high_green = np.array([75, 255, 255])
    mask_green = cv2.inRange(hsv_frame, low_yesil, high_yesil)
    #mavi
    low_blue = np.array([94, 80, 2])
    high_blue = np.array([126, 255, 255])
    mask_blue = cv2.inRange(hsv_frame, low_blue, high_blue)

    kernal = np.ones((5, 5), "uint8")

    #kırmızı
    mask_red = cv2.dilate(mask_red, kernal)
    res_red = cv2.bitwise_and(frame, frame, mask= mask_red)
    #yeşil
    mask_green = cv2.dilate(mask_green, kernal)
    res_green = cv2.bitwise_and(frame, frame, mask= mask_green)
    #mavi
    mask_blue = cv2.dilate(mask_blue, kernal)
    res_blue = cv2.bitwise_and(frame, frame, mask= mask_blue)



    contours = cv2.findContours(mask_blue,
                                           cv2.RETR_EXTERNAL,
                                           cv2.CHAIN_APPROX_SIMPLE)
    contours = contours[0] if len(contours) == 2 else contours[1]
    for contour in enumerate(contours):  
        area=cv2.contourArea(contour)
        if(area > 300):
            x, y,w, h = cv2.boundingRect(contour)
            frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
            cv2.putText(frame, "blue", (x, y), cv2.Font_HERSHEY_SIMPLEX, 1.0, (0, 0,255), 1)
            port.digital[13].write(1)
        else:
            port.digital[13].write(0)



    contours, hierarchy= cv2.findContours(mask_green,
                                                    cv2.RETR_EXTERNAL,
                                                    cv2.CHAIN_APPROX_SIMPLE)
    contours = cnts.astype(np.uint32)
    for pic, contour in enumerate(contours):
        area = cv2.contourArea(contour)
        if(area > 300):
            x, y,w, h = cv2.boundingRect(contour)
            frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
            cv2.putText(frame, "green", (x, y), cv2.Font_HERSHEY_SIMPLEX, 1.0, (0, 0,255), 1)

    cv2.imshow("Multiple Color Detection İn Real Time", frame)
    if cv2.waitKey(10) & 0xFF == ord('q'):
        cap.realse()
        cv2.destroyALLWindows()
        break
cv2.destroyALLWindows()

Traceback (most recent call last):
File "C:\Users\erkan\AppData\Local\Programs\Python\Python39\kamera1.py", line 49, in
area=cv2.contourArea(contour)
cv2.error: OpenCV(4.5.3) 👎 error: (-5:Bad argument) in function 'contourArea'
Overload resolution failed

  • contour is not a numerical tuple
  • Expected Ptrcv::UMat for argument 'contour'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.