Real-Time Bottle Level Detection Application

Hi,
I can determine the level with bottle photos. But I can’t do this in real time. The program is not working. Photo bottle level detection code and real-time bottle level detection code;

PHOTO BOTTLE LEVEL DETECTION
import cv2

from matplotlib import pyplot as plt

import imutils

bottle = cv2.imread("veri1/foto2.jpg")

bottle_gray = cv2.split(bottle)[0]

#blurlama işlemi

bottle_gray = cv2.GaussianBlur(bottle_gray, (7,7), 0)

#draw histogram

#plt.hist(bottle_gray.ravel(), 256, [0,256]); plt.show()

#manuel threshold

(T, bottle_threshold) = cv2.threshold(bottle_gray, 30.5, 250, cv2.THRESH_BINARY_INV)

#apply opening operation

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))

bottle_open = cv2.morphologyEx(bottle_threshold, cv2.MORPH_OPEN, kernel)

#find all contours

contours = cv2.findContours(bottle_open.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

contours = imutils.grab_contours(contours)

bottle_clone = bottle.copy()

cv2.drawContours(bottle_clone, contours, -1, (255,0,0), 2)

#sort contours by area

areas = [cv2.contourArea(contour) for contour in contours]

(contours, areas) = zip(*sorted(zip(contours,areas),key = lambda a:a[1]))

#print contour with largest area

bottle_clone = bottle.copy()

cv2.drawContours(bottle_clone, [contours[-1]], -1, (255,0,0), 2)

#draw bounding box, calculate aspect and display decision

bottle_clone = bottle.copy()

(x, y ,w, h) = cv2.boundingRect(contours[-1])

aspectRatio = w / float(h)

if aspectRatio < 0.4:

cv2.rectangle(bottle_clone, (x,y), (x+w, y+h), (0,255,0), 2)

cv2.putText(bottle_clone, "Dolu", (x+10, y+20), cv2.FONT_HERSHEY_PLAIN, 1, (0,255,0), 2)

else:

cv2.rectangle(bottle_clone, (x,y), (x+w, y+h), (0,0,255), 2)

cv2.putText(bottle_clone, "Eksik", (x+10, y+20), cv2.FONT_HERSHEY_PLAIN, 1, (0,0,255), 2)

cv2.imshow("Bottle Threshold", bottle_threshold)

cv2.imshow("Decision", bottle_clone)

cv2.waitKey(0)

REAL-TIME BOTTLE LEVEL DETECTION (NOT WORKING)

import cv2
import numpy as np 
import RPi.GPIO as GPIO
import time
from matplotlib import pyplot as plt 
import imutils

def gstreamer_pipeline(
    capture_width=1280,
    capture_height=720,
    display_width=600,
    display_height=400,
    framerate=0,
    flip_method=0,
):
    return (
        "nvarguscamerasrc ! "
        "video/x-raw(memory:NVMM), "
        "width=(int)%d, height=(int)%d, "
        "format=(string)NV12, framerate=(fraction)%d/1 ! "
        "nvvidconv flip-method=%d ! "
        "video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! "
        "videoconvert ! "
        "video/x-raw, format=(string)BGR ! appsink"
        % (
            capture_width,
            capture_height,
            framerate,
            flip_method,
            display_width,
            display_height,
        )
    )

cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER) # 6.- 31. arası kamera devreye al

while True: 
    _, frame = cap.read()
    #hsv_frame = cv2.cvtColor(frame, cv2.COLOR.BGR2HSV)

    bottle_gray = cv2.split(frame)[0]
    bottle_gray = cv2.GaussianBlur(bottle_gray, (7,7), 0)

    (T, bottle_threshold) = cv2.threshold(bottle_gray, 30.5, 250, cv2.THRESH_BINARY_INV)

    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
    bottle_open = cv2.morphologyEx(bottle_threshold, cv2.MORPH_OPEN, kernel)
    contours = cv2.findContours(bottle_open.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    contours = imutils.grab_contours(contours)
    bottle_clone = frame.copy()
    cv2.drawContours(bottle_clone, contours, -1, (255,0,0), 2)
    areas = [cv2.contourArea(contour) for contour in contours]
    (contours, areas) = zip(*sorted(zip(contours,areas),key = lambda a:a[1]))

    bottle_clone1 = frame.copy()
    cv2.drawContours(bottle_clone1, [contours[-1]], -1, (255,0,0), 2)
    bottle_clone2 = frame.copy()
    (x, y ,w, h) = cv2.boundingRect(contours[-1])
    aspectRatio = w / float(h)

    if aspectRatio < 0.9:
        cv2.rectangle(bottle_clone2, (x,y), (x+w, y+h), (0,255,0), 2)
        cv2.putText(bottle_clone2, "Dolu", (x+10, y+20), cv2.FONT_HERSHEY_PLAIN, 1, (0,255,0), 2)
    else:
        cv2.rectangle(bottle_clone2, (x,y), (x+w, y+h), (0,0,255), 2)
        cv2.putText(bottle_clone2, "Eksik", (x+10, y+20), cv2.FONT_HERSHEY_PLAIN, 1, (0,0,255), 2)

    #cv2.imshow("Bottle Threshold", bottle_threshold)
    cv2.imshow("Decision", bottle_clone2)

cv2.waitKey(0)
cap.release()
cv2.destroyAllWindows()

Please help. Thanks in advance

Hi,

Which kind of ‘not working’ do you mean?
Do you meet any error? If yes, could you share the error log with us?

Thanks.