Camera blockage detection that is mounted on a car [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I'm developing an algorithm that detects a camera blockage using python OpenCV.
Actually, as I'm not good at computer vision yet i'm not sure if the algorithm I came up with is appropriate.
Can anybody take a look at the code I made?
def detectCameraBlockage_absdiff():
cnt = 0
cap = cv2.VideoCapture(0)
# print('width: ', cap.get(3))
# print('height: ', cap.get(4))
IMAGE_WIDTH = cap.get(3)
IMAGE_HEIGHT = cap.get(4)
background = np.zeros((480, 640))
while True:
if cnt == 0:
cnt += 1
pass
else:
ret, frame = cap.read()
# frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
if ret:
cv2.imshow('if you want to quit press the key [q]', frame)
# cv2.imshow('if you want to quit press the key [q]', frame[:, :, 0])
mean_frame = frame.mean(axis=2)
# cv2.imshow('mean_frame', mean_frame)
diff = cv2.absdiff(background, mean_frame)
# print(type(diff))
cv2.imshow('diff', diff)
_, diff = cv2.threshold(diff, 30, 255, cv2.THRESH_BINARY)
# print(type(_), type(diff), sep='\n') #float, ndarray
if cnt%10 == 0:
background = mean_frame
cnt += 1
print('cnt: ', cnt)
if cv2.waitKey(1) == ord('q'):
break
else:
print('camera failed.')
break
This code doesn't work for me.
I cannot think out how should i deal with that pixel values to get the difference between the image that has no blockage and that has.

An idea would be to measure the similarity between a reference image and the current image. The reference image would be the image without the blockage.
If your camera is static, a simple way to implement it is to use SSIM (https://docs.opencv.org/4.5.2/d5/dc4/tutorial_video_input_psnr_ssim.html).

Related

OpenCV imshow error with webcam: (-215:Assertion failed) [duplicate]

I am trying to make a face tracker that combines Haar Cascade Classification with Lucas Kanade good feature detection. However, I keep getting an error that I cannot figure out what it means nor how to solve it.
Can anyone help me here?
Error:
line 110, in <module>
cv2.imshow('frame',img)
error: /build/buildd/opencv-2.4.8+dfsg1/modules/highgui/src/window.cpp:269:
error: (-215)size.width>0 && size.height>0 in function imshow
Code:
from matplotlib import pyplot as plt
import numpy as np
import cv2
face_classifier = cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
# params for ShiTomasi corner detection
feature_params = dict( maxCorners = 200,
qualityLevel = 0.01,
minDistance = 10,
blockSize = 7 )
# Parameters for lucas kanade optical flow
lk_params = dict( winSize = (15,15),
maxLevel = 2,
criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))
# Create some random colors
color = np.random.randint(0,255,(100,3))
# Take first frame and find corners in it
ret, old_frame = cap.read()
cv2.imshow('Old_Frame', old_frame)
cv2.waitKey(0)
old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)
restart = True
#while restart == True:
face = face_classifier.detectMultiScale(old_gray, 1.2, 4)
if len(face) == 0:
print "This is empty"
for (x,y,w,h) in face:
focused_face = old_frame[y: y+h, x: x+w]
cv2.imshow('Old_Frame', old_frame)
face_gray = cv2.cvtColor(old_frame,cv2.COLOR_BGR2GRAY)
gray = cv2.cvtColor(focused_face,cv2.COLOR_BGR2GRAY)
corners_t = cv2.goodFeaturesToTrack(gray, mask = None, **feature_params)
corners = np.int0(corners_t)
print corners
for i in corners:
ix,iy = i.ravel()
cv2.circle(focused_face,(ix,iy),3,255,-1)
cv2.circle(old_frame,(x+ix,y+iy),3,255,-1)
plt.imshow(old_frame),plt.show()
# Create a mask image for drawing purposes
mask = np.zeros_like(old_frame)
while(1):
ret,frame = cap.read()
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# calculate optical flow
p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, corners_t, None, **lk_params)
# Select good points
good_new = p1[st==1]
good_old = corners_t[st==1]
# draw the tracks
print "COLORING TIME!"
for i,(new,old) in enumerate(zip(good_new,good_old)):
print i
print color[i]
a,b = new.ravel()
c,d = old.ravel()
mask = cv2.line(mask, (a,b),(c,d), color[i].tolist(), 2)
frame = cv2.circle(frame,(a, b),5,color[i].tolist(),-1)
if i == 99:
break
img = cv2.add(frame,mask)
cv2.imshow('frame',img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
# Now update the previous frame and previous points
old_gray = frame_gray.copy()
p0 = good_new.reshape(-1,1,2)
cv2.destroyAllWindows()
cap.release()
This error message
error: (-215)size.width>0 && size.height>0 in function imshow
simply means that imshow() is not getting video frame from input-device.
You can try using
cap = cv2.VideoCapture(1)
instead of
cap = cv2.VideoCapture(0)
& see if the problem still persists.
I have the same problem, fix the ret in capture video
import numpy as np
import cv2
# Capture video from file
cap = cv2.VideoCapture('video1.avi')
while True:
ret, frame = cap.read()
if ret == True:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(30) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()
I had this problem.
Solution: Update the path of the image.
If the path contains (for example: \n or \t or \a) it adds to the corruption. Therefore, change every back-slash "\" with front-slash "/" and it will not make create error but fix the issue of reading path.
Also double check the file path/name. any typo in the name or path also gives the same error.
You have to delay
Example Code:
import cv2
import numpy as np
import time
cam = cv2.VideoCapture(0)
time.sleep(2)
while True:
ret,frame = cam.read()
cv2.imshow('webcam', frame)
if cv2.waitKey(1)&0xFF == ord('q'):
break
cam.release()
cv2.destroyAllWindows()
I have also met this issue. In my case, the image path is wrong, so the img read is NoneType. After I correct the image path, I can show it without any issue.
In these two lines:
mask = cv2.line(mask, (a,b),(c,d), color[i].tolist(), 2)
frame = cv2.circle(frame,(a, b),5,color[i].tolist(),-1)
try instead:
cv2.line(mask, (a,b),(c,d), color[i].tolist(), 2)
cv2.circle(frame,(a, b),5,color[i].tolist(),-1)
I had the same problem and the variables were being returned empty
I also met the error message in raspberry pi 3, but my solution is reload kernel of camera after search on google, hope it can help you.
sudo modprobe bcm2835-v4l2
BTW, for this error please check your camera and file path is workable or not
That error also shows when the video has played fine and the script will finish but that error always throws because the imshow() will get empty frames after all frames have been consumed.
That is especially the case if you are playing a short (few sec) video file and you don't notice that the video actually played on the background (behind your code editor) and after that the script ends with that error.
while(cap.isOpened()):
ret, img = cap.read()
print img
if img==None: #termino los frames?
break #si, entonces terminar programa
#gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('img2',img)
cv2.circle and cv2.lines are not working. Mask and frame both are returning None. these functions (line and circle) are in opencv 3 but not in older versions.
I use ssh to connect to remote server and have python code execute cv2.VideoCapture(0) to capture remote webcam, then encounter this error message:
error: (-215)size.width>0 && size.height>0 in function imshow
Finally, I have to grant access to /dev/video0 (which is my webcam device) with my user account and the error message was gone. Use usermod to add user into group video
usermod -a -G video user
This is a problem with space consumption or choosing the wrong camera.
My suggestion in to restart kernel and clear output and run it again.
It works then.
Although this is an old thread, I got this error as well and the solution that worked for me is not mentioned here.
Simply put, in my case the webcam was still in use on the background, as I saw the LED light being on. I have not yet been able to reproduce the issue, so I'm not sure a simple cv2.VideoCapture(0).release() would have solved it. I'll edit this post if and when I have found it out.
For me a restart of my PC solved the issue, without changing anything to the code.
This Error can also occur if you slice a negative point and pass it to the array. So check if you did
I was facing the same problem while trying to open images containing spaces and special
characters like the following ยด in their names
So, after modifying the images names removing their spaces and special characters, everything worked perfectly.
Check if you have "opencv_ffmpeg330.dll" in python27 root directory or of the python version you are using. If not you will find it in "....\OpenCV\opencv\build\bin".
Choose the appropriate version and copy the dll into the root directory of your python installation and re-run the program
Simply use an image extension like .jpeg or .png.

Webcam based object Scanner in C# 9/10 .net 6 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Im trying to port this Python code from theDataFox card_scanner_app to an C# WPF Application.
The Python Programm uses a webcam to recognize a Object (Trading Card) and makes a Snapshot of the Object by pressing the Space bar.
As i know the Code Hashes the Snapshot and compares it to a database of 64.000 Images wich are already hashed.
Then the Code uses the Image ID, given in the Image Filename and generates the Output from the Database with all Informations about this specific Object.
import cv2
import pygame
import imutils
import numpy as np
import pygame.camera
from pygame.locals import KEYDOWN, K_q, K_s, K_SPACE
from . import util
import argparse
import glob
DEBUG = False
# calibrate this for camera position
MIN_CARD_AREA = 250000.0 / 3
# calibrate these
THRESHOLD = (100, 255)
FILTER = (11, 17, 17)
ROTATION = 0
# noinspection PyUnresolvedReferences
def scan(img):
# preprocess image
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.bilateralFilter(gray, FILTER[0], FILTER[1], FILTER[2])
ret, gray = cv2.threshold(gray, THRESHOLD[0], THRESHOLD[1], cv2.THRESH_BINARY)
edges = imutils.auto_canny(gray)
# extract contours
cnts, _ = cv2.findContours(edges.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = [c for c in cnts if cv2.contourArea(c) >= MIN_CARD_AREA]
card, c = None, None
if cnts:
# get largest contour
c = sorted(cnts, key=cv2.contourArea, reverse=True)[0]
# approximate the contour
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.05 * peri, True)
pts = np.float32(approx)
x, y, w, h = cv2.boundingRect(c)
# Find center point of card by taking x and y average of the four corners.
# average = np.sum(pts, axis=0)/len(pts)
# cent_x = int(average[0][0])
# cent_y = int(average[0][1])
# center = [cent_x, cent_y]
# Warp card into 200x300 flattened image using perspective transform
card = util.flattener(img, pts, w, h)
card = util.cv2_to_pil(card).rotate(ROTATION)
return card, c, gray, edges
# noinspection PyUnresolvedReferences
def detect(on_detect):
dim = (800, 600)
pygame.init()
pygame.camera.init()
cams = pygame.camera.list_cameras()
# print(cams)
display = pygame.display.set_mode(dim, 0)
cam = pygame.camera.Camera(cams[-1], dim)
cam.start()
capture = True
while capture:
img = cam.get_image()
img = pygame.transform.scale(img, dim)
img = util.pygame_to_cv2(img)
card, c, gray, edges = scan(img)
# q to quit
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_q:
capture = False
elif event.key == K_s or event.key == K_SPACE:
if card is None:
print('nothing found')
else:
on_detect(card)
# display
if c is not None:
cv2.drawContours(img, [c], -1, (0, 0, 255), 3)
img = util.cv2_to_pygame(img)
display.blit(img, (0, 0))
pygame.display.update()
if card is not None:
card_img = util.pil_to_pygame(card)
display.blit(card_img, (0, 0))
if DEBUG:
for layer in [gray, edges]:
layer = cv2.cvtColor(layer, cv2.COLOR_GRAY2RGB)
layer = util.cv2_to_pygame(layer)
layer.set_alpha(100)
display.blit(layer, (0, 0))
pygame.display.flip()
cam.stop()
pygame.quit()
Now i'm trying to find a Solution to get similar Webcam Access with C# (Visual Studio).
As to i'm still an Beginner I have no Idea how to do this.
I tried several NuGet Packages and got nothing working. I guess my skills aren't good enough.
Therefor I now have to ask you, do you have any Ideas or Solutions that could do the given task?
I already ported most of the Programm and it's working fine.
This is my full Code
There is no built in webcam api directly in c#, the closest you can get is the UWP video capture API. When I tried this, it did not work very well with my webcamera, but that might just be me.
There are lots of image processing libraries that have webcamera support built in. This includes OpenCV/emguCV, but also for example aforge.
I have used a "versatile webcam library" that is a wrapper around the native webcam APIs, and it seem to work well.
More or less all of the libraries have good documentation with nice examples how to get images, many also have sample applications that demonstrate how to use the library, and this should be plenty to get you started. So "I tried and it didn't work" is not a good explanation. What have you tried? Why did it not work?

Face Detection with less cpu load cv2 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
for a university project I am programming a face mask recognition. For detecting faces, I use the cv2.CascadeClassifier('face_detector.xml'). As I noticed, this program is taking up way too much of the CPU resulting in a heavily disordered video stream frame rate.
I am running the code on a MacBook Air with a 1.6Hz Dual Core (Intel Core i5).
Can someone explain what I can change to make it smoother? Or maybe recommend another face detection?
Here is my code:
import numpy as np
import os
import tensorflow as tf
import cv2
from matplotlib.pyplot import gray
# Disable tensorflow compilation warnings
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import cv2
# Load the cascade
face_cascade = cv2.CascadeClassifier('face_detector.xml')
# To capture video from webcam.
cap = cv2.VideoCapture(0)
# To use a video file as input
# cap = cv2.VideoCapture('filename.mp4')
model = tf.keras.models.load_model('checkpoint19.ckpt')
i = 0
while True:
# Read the frame
_, img = cap.read()
# Detect the faces
faces = face_cascade.detectMultiScale(img, 1.3, 4)
# save each frame as image with PNG format
image = cv2.imwrite('database/{index}.png'.format(index=i), img)
i += 1
# cut out the fragment in the box of the image
# Draw the rectangle around each face
for (x, y, w, h) in faces:
crop_img = img[y:y + h, x:x + w]
resizedImg = cv2.resize(crop_img, (224, 224))
gray = cv2.cvtColor(resizedImg, cv2.COLOR_BGR2GRAY)
imgArrNew = gray.reshape(1, 224, 224, 1)
prediction = model.predict(imgArrNew)
print(prediction)
label = np.argmax(prediction)
print(label)
# font
font = cv2.FONT_HERSHEY_SIMPLEX
# org
for (x, y, w, h) in faces:
org = (x, y+h+30)
# fontScale
fontScale = 1
# Blue color in BGR
color = (255, 0, 0)
# Line thickness of 2 px
thickness = 2
# output the predicted label/sign on the live-stream frame
if label == 0:
color = (0,0,225)
label_out = "Mask off"
if label == 1:
color = (50,205,50)
label_out = "Mask on"
if label == 2:
color = (0,255,225)
label_out = "incorrect Mask"
cv2.rectangle(img, (x, y), (x + w, y + h), color, 2)
image1 = cv2.putText(img, label_out, org, font,
fontScale, color, thickness, cv2.LINE_AA)
# Display
cv2.imshow('Face_Regonition', img)
# Stop if escape key is pressed
k = cv2.waitKey(30) & 0xff
if k == 27:
break
# Release the VideoCapture object
cap.release()
Thanks for your help :)
haar cascaded classifier is slow. . To do detection in every single frame is hard for low-end computing devices.
The easiest way is to use a lower resolution image or lower FPS. But it will appear to be cheap
The better way is to use a detection and tracking framework where detection happens at a 1hz interval at a new thread and tracking can happen at 30hz, which human eye cant tell the difference.
For detection of face, you can choose any method such as hear, HOG, CNN and put it in a new thread. In the main tracking thread (which can run in real time) update the model and predict the bounding box and display it.
You may look for the tracking from here. I suggest KCF based method for it is fast and reliable.
https://www.pyimagesearch.com/2018/07/30/opencv-object-tracking/
Just put the detection box rect as input rect box for the tracking. THen it should work directly.

How to split a video frame into 9 pieces? [duplicate]

This question already has an answer here:
opencv using python - copy roi to new smaller image
(1 answer)
Closed 5 years ago.
I am using Opencv2 in Python3.
I'm writing "shot boundary detection" program for news videos.
I've just need to split a video frame into 9 pieces for the beginning. But, i'm not sure how to do that cus i'm new in this field. I did it with cropping but it seems to me that it is not right thing to do to split a frame into pieces.
Note: When i search with "my title", i couldn't reach the ROI answer. But it now seems that this is ROI.
You need to use ROI's and add the different images in an array for example.
I tested with my cat:
Here you have the code: (Press key 'q' to finish it)
import cv2
cap = cv2.VideoCapture(0)
n_rows = 3
n_images_per_row = 3
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
height, width, ch = frame.shape
roi_height = height / n_rows
roi_width = width / n_images_per_row
images = []
for x in range(0, n_rows):
for y in range(0,n_images_per_row):
tmp_image=frame[x*roi_height:(x+1)*roi_height, y*roi_width:(y+1)*roi_width]
images.append(tmp_image)
# Display the resulting sub-frame
for x in range(0, n_rows):
for y in range(0, n_images_per_row):
cv2.imshow(str(x*n_images_per_row+y+1), images[x*n_images_per_row+y])
cv2.moveWindow(str(x*n_images_per_row+y+1), 100+(y*roi_width), 50+(x*roi_height))
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

opencv/python : motion detect weird thresholding

I am trying to make a motion detect program using my webcam, but I'm getting this weird results when thresholding the difference of frames:
When Im moving: (seems okay I guess)
![enter image description here][1]
When Im not moving:
![enter image description here][2]
What can this be? I already ran a couple of programs that got exactly the same algorithm and the thresholding is doing fine..
Heres my code:
import cv2
import random
import numpy as np
# Create windows to show the captured images
cv2.namedWindow("window_a", cv2.CV_WINDOW_AUTOSIZE)
cv2.namedWindow("window_b", cv2.CV_WINDOW_AUTOSIZE)
# Structuring element
es = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9,4))
## Webcam Settings
capture = cv2.VideoCapture(0)
#dimensions
frameWidth = capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)
frameHeight = capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)
while True:
# Capture a frame
flag,frame = capture.read()
current = cv2.blur(frame, (5,5))
difference = cv2.absdiff(current, previous) #difference is taken of the current frame and the previous frame
frame2 = cv2.cvtColor(difference, cv2.cv.CV_RGB2GRAY)
retval,thresh = cv2.threshold(frame2, 10, 0xff, cv2.THRESH_OTSU)
dilated1 = cv2.dilate(thresh, es)
dilated2 = cv2.dilate(dilated1, es)
dilated3 = cv2.dilate(dilated2, es)
dilated4 = cv2.dilate(dilated3, es)
cv2.imshow('window_a', dilated4)
cv2.imshow('window_b', frame)
previous = current
key = cv2.waitKey(10) #20
if key == 27: #exit on ESC
cv2.destroyAllWindows()
break
Thanks in advance!
[1]: http://i.stack.imgur.com/hslOs.png
[2]: http://i.stack.imgur.com/7fB95.png
The first thing that you need is a previous = cv2.blur(frame, (5,5)) to prime your previous sample after a frame grab before your while loop.
This will make the code you posted work, but will not solve your problem.
I think the issue that you are having is due to the type of thresholding algorithm that you are using. Try a binary, cv2.THRESH_BINARY, instead of Otsu's. It seemed to solve the problem for me.

Categories