What I'm trying to make:
A OpenCV program that can record a video and mask my face by using an image of a mask.
My Code:
import numpy as np
import cv2
import os
import time
import face_recognition as fr
import pkg_resources
filename = "THIS_IS_A_TEST.mp4"
frames_per_seconds = 24.0
my_res = "720p"
face_cascade = cv2.CascadeClassifier('C:\\Users\\jack\\Desktop\\haarcascade_frontalface_default.xml')
#eyes_cascade = cv2.CascadeClassifier('C:\\Users\\jack\\Desktop\\frontalEyes35x16.xml')
mask = cv2.imread("C:\\Users\\jack\\Desktop\\Blogger_Video_Recorder\\TheMask.png", -1)
def change_res(cap, width, height):
cap.set(3, width)
cap.set(4, height)
STD_DIMENSIONS = {
"480p": (640,480),
"720p": (1280, 720),
"1080p": (1920, 1080),
"4k": (3840, 2160),
}
def image_resize(image, width = None, height = None, inter = cv2.INTER_AREA):
# initialize the dimensions of the image to be resized and
# grab the image size
dim = None
(h, w) = image.shape[:2]
# if both the width and height are None, then return the
# original image
if width is None and height is None:
return image
# check to see if the width is None
if width is None:
# calculate the ratio of the height and construct the
# dimensions
r = height / float(h)
dim = (int(w * r), height)
# otherwise, the height is None
else:
# calculate the ratio of the width and construct the
# dimensions
r = width / float(w)
dim = (width, int(h * r))
# resize the image
resized = cv2.resize(image, dim, interpolation = inter)
# return the resized image
return resized
def get_dims(cap, res="1080p"):
width, height = STD_DIMENSIONS["480p"]
if res in STD_DIMENSIONS:
width, height = STD_DIMENSIONS[res]
change_res(cap, width, height)
return width, height
VIDEO_TYPE = {
"mp4": cv2.VideoWriter_fourcc(*"XVID")
}
def get_video_type(filename):
filename, ext = os.path.splitext(filename)
if ext in VIDEO_TYPE:
return VIDEO_TYPE[ext]
return VIDEO_TYPE["mp4"]
cap = cv2.VideoCapture(0)
dims = get_dims(cap, res = my_res)
video_type_cv2 = get_video_type(filename)
out = cv2.VideoWriter(filename, video_type_cv2, frames_per_seconds, dims)
while True:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.5, minNeighbors=5)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
for (x, y, w, h) in faces:
roi_gray = gray[y:y+h, x:x+h]
roi_color = frame[y:y+h, x:x+h]
roi_faces = roi_gray[y:y+h, x:x+w]
mask2 = image_resize(mask.copy(), width=w)
mw, mh, mc = mask2.shape
for i in range(0,mw):
for j in range(0, mh):
if mask2[i, j][3] != 0:
roi_color[y + i, x + j] = mask2[i, j]
frame = cv2.cvtColor(frame, cv2.COLOR_BGRA2BGR)
out.write(frame)
cv2.imshow("frame", frame)
if cv2.waitKey(20) & 0xFF == ord("q"):
break
cap.release()
out.release()
cv2.destroyAllWindows()
Traceback (most recent call last):
File "C:\Users\jack\Desktop\Blogger_Video_Recorder\tutorial#1.py", line 93, in <module>
roi_color[y + i, x + j] = mask2[i, j]
IndexError: index 426 is out of bounds for axis 0 with size 273
BTW I am also running this on the Python IDLE, so any help is appreciated :)
You do not have to do most of the existing phases. You can use the following method:
draw[y:y+h, x:x+w] = mask2
But in reality there are several errors, the first of which is that the height of the mask is less than the height of the face, so we will not be able to use the values surrounding the face that were found
However, it is possible to know the mask height and reset the print dimensions on the basic frame.
The next line
mask2 = image_resize(mask.copy(), width=w)
mw, mh, mc = mask2.shape
draw[y:y+mw, x:x+w] = mask2
Note Please do not use 'cv2.COLOR_BGR2BGRA' Or the conversion line:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2BGRA)
Except for necessity, because it increases the depth of the image to 4 dimensions, and an error will occur when you start printing the mask with the three dimensions,Like:
ValueError: could not broadcast input array from shape (273,410,3) into shape (273,410,4)
Of course it is possible to find another solution but it remains unlikely because you did not use BGRA at all in this code.
Imagine ->full code:
import numpy as np
import cv2
import os
import time
import face_recognition as fr
import pkg_resources
filename = "THIS_IS_A_TEST.mp4"
frames_per_seconds = 24.0
my_res = "720p"
face_cascade = cv2.CascadeClassifier(os.path.join(cv2.data.haarcascades ,'haarcascade_frontalface_default.xml'))
#eyes_cascade = cv2.CascadeClassifier(os.path.join(cv2.data.haarcascades ,'frontalEyes35x16.xml')
mask = cv2.imread("test.jpg", -1)
def change_res(cap, width, height):
cap.set(3, width)
cap.set(4, height)
STD_DIMENSIONS = {
"480p": (640,480),
"720p": (1280, 720),
"1080p": (1920, 1080),
"4k": (3840, 2160),
}
def image_resize(image, width = None, height = None, inter = cv2.INTER_AREA):
# initialize the dimensions of the image to be resized and
# grab the image size
dim = None
(h, w) = image.shape[:2]
# if both the width and height are None, then return the
# original image
if width is None and height is None:
return image
# check to see if the width is None
if width is None:
# calculate the ratio of the height and construct the
# dimensions
r = height / float(h)
dim = (int(w * r), height)
# otherwise, the height is None
else:
# calculate the ratio of the width and construct the
# dimensions
r = width / float(w)
dim = (width, int(h * r))
# resize the image
resized = cv2.resize(image, dim, interpolation = inter)
# return the resized image
return resized
def get_dims(cap, res="1080p"):
width, height = STD_DIMENSIONS["480p"]
if res in STD_DIMENSIONS:
width, height = STD_DIMENSIONS[res]
change_res(cap, width, height)
return width, height
VIDEO_TYPE = {
"mp4": cv2.VideoWriter_fourcc(*"XVID")
}
def get_video_type(filename):
filename, ext = os.path.splitext(filename)
if ext in VIDEO_TYPE:
return VIDEO_TYPE[ext]
return VIDEO_TYPE["mp4"]
cap = cv2.VideoCapture(0)
dims = get_dims(cap, res = my_res)
video_type_cv2 = get_video_type(filename)
out = cv2.VideoWriter(filename, video_type_cv2, frames_per_seconds, dims)
while True:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.5, minNeighbors=5)
draw = frame.copy()
for (x, y, w, h) in faces:
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]
roi_faces = roi_gray[y:y+h, x:x+w]
mask2 = image_resize(mask.copy(), width=w)
mw, mh, mc = mask2.shape
draw[y:y+mw, x:x+w] = mask2
out.write(draw)
cv2.imshow("frame", draw)
if cv2.waitKey(5) & 0xFF == ord("q"):
break
cap.release()
out.release()
cv2.destroyAllWindows()
Side note:
It is best to use the expression cv2.data.haarcascades to find the path of the xml files included in the library
can be used os.path.join(cv2.data.haarcascades ,'u_file.xml') to find a valid path for most operating systems.
example of this
import os
import cv2
root_data = cv2.data.haarcascades
face_cascade = cv2.CascadeClassifier(os.path.join(root_data ,'haarcascade_frontalface_default.xml'))
eyes_cascade = cv2.CascadeClassifier(os.path.join(root_data ,'frontalEyes35x16.xml'))
Done..
Related
The program is written for the application and displays the broadcast from the camera video, however, I came across the fact that the output is in some unspecified encoding (format) look photo
class Worker1(QThread):
ImageUpdate = pyqtSignal(QImage)
def run(self):
self.ThreadActive = True
Capture = cv2.VideoCapture(0)
while self.ThreadActive:
face_cascade = cv2.CascadeClassifier('cascade/haarcascade_russian_plate_number.xml')
ret, frame = Capture.read()
if ret:
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
with_cascade = face_cascade.detectMultiScale(image, 1.3, 7)
for i, (x, y, w, h) in enumerate(with_cascade):
roi_color = frame[y:y + h, x:x + w]
r = 300.0 / roi_color.shape[1]
dim = (400, int(roi_color.shape[0] * r))
resized = cv2.resize(roi_color, dim, interpolation=cv2.INTER_AREA)
w_resized = resized.shape[0]
h_resized = resized.shape[1]
frame[380:380 + w_resized, 235:235 + h_resized] = resized
convert_to_qt_format = QImage(frame.data, frame.shape[1], frame.shape[0], QImage.Format_RGB888)
self.ImageUpdate.emit(convert_to_qt_format)
def image_update_slot(self, Image):
self.ui.Cam.setPixmap(QPixmap.fromImage(Image))
self.ui.Cam.setScaledContents(True)
How can i fix this problem?
photo with problem:
Maybe there are problems with the image format, but if I change it, the programme will be closed
i follow a tutorial to learn how to use opencv which and a practical library with tensorflow notably for the cnn but I have an error
Traceback (most recent call last): File "watermark.py", line 15, in <module> watermark =
image_resize(logo, height=50) File "C:\Users\HP\Desktop\Private\project\cv\utils.py", line 9, in
image_resize (h, w) = image.shape:2 AttributeError: 'NoneType' object has no attribute 'shape' WARN:0
global C:\projects\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (674)
SourceReaderCB::~SourceReaderCB terminating async callback
code
utils.py
import cv2
import os
def image_resize(image, width = None, height = None, inter = cv2.INTER_AREA):
dim = None
(h, w) = image.shape[:2]
if width is None and height is None:
return image
if width is None:
r = height / float(h)
dim = (int(w * r), height)
else:
r = width / float(w)
dim = (width, int(h * r))
resized = cv2.resize(image, dim, interpolation = inter)
return resized
class CFEVideoConf(object):
STD_DIMENSIONS = {
"360p": (480, 360),
"480p": (640, 480),
"720p": (1280, 720),
"1080p": (1920, 1080),
"4k": (3840, 2160),
}
VIDEO_TYPE = {
'avi': cv2.VideoWriter_fourcc(*'XVID'),
'mp4': cv2.VideoWriter_fourcc(*'XVID'),
}
width = 640
height = 480
dims = (640, 480)
capture = None
video_type = None
def __init__(self, capture, filepath, res="480p", *args, **kwargs):
self.capture = capture
self.filepath = filepath
self.width, self.height = self.get_dims(res=res)
self.video_type = self.get_video_type()
def change_res(self, width, height):
self.capture.set(3, width)
self.capture.set(4, height)
def get_dims(self, res='480p'):
width, height = self.STD_DIMENSIONS['480p']
if res in self.STD_DIMENSIONS:
width, height = self.STD_DIMENSIONS[res]
self.change_res(width, height)
self.dims = (width, height)
return width, height
def get_video_type(self):
filename, ext = os.path.splitext(self.filepath)
if ext in self.VIDEO_TYPE:
return self.VIDEO_TYPE[ext]
return self.VIDEO_TYPE['avi']
watermark.py
import numpy as np
import cv2
from utils import CFEVideoConf, image_resize
cap = cv2.VideoCapture(0)
save_path = 'saved-media/watermark.mp4'
frames_per_seconds = 24
config = CFEVideoConf(cap, filepath=save_path, res='720p')
out = cv2.VideoWriter(save_path, config.video_type, frames_per_seconds, config.dims)
img_path = 'images/logo/1.png'
logo = cv2.imread(img_path, -1)
watermark = image_resize(logo, height=50)
cv2.imshow('watermark',watermark)
while(True):
ret, frame = cap.read()
print(frame[50,150])
color=(255,0,0)
start_cord_x=50
start_cord_y=150
stroke=2
w=100
h=200
end_cord_x=start_cord_x+w
end_cord_y=start_cord_y+h
cv2.rectangle(frame,(x,y),(end_cord_x,end_cord_y),color,stroke)
cv2.imshow('frame',frame)
if cv2.waitKey(20) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
thanks for helping me found the error
the expected result is that the logo image 1.png when the camera turns on and the video download shows both the logo + the standard result
Here's your problem, you're reading an image from a non-existent file, hence the empty image that has no shape and is NoneType:
img_path = 'images/logo/1.png'
logo = cv2.imread(img_path, -1)
I have 2 images as below, both are in RGBA format
I want to overlay the object into a fixed region in the background. However, my codes produces a weird result:
Can someone have a look at my code to see where I was wrong. Thanks in advance
import numpy as np
import matplotlib.pyplot as plt
import cv2
import glob
from PIL import Image
bg = np.array(Image.open('background.png'))
obj = np.array(Image.open('object.png'))
#RESIZE BACKGROUND
scale_percent = 50 # percent of original size
width = int(bg.shape[1] * scale_percent / 100)
height = int(bg.shape[0] * scale_percent / 100)
dim = (width, height)
bg = resized = cv2.resize(bg, dim, interpolation = cv2.INTER_AREA)
#RESIZE OBJECT
scale_percent = 80 # percent of original size
width = int(obj.shape[1] * scale_percent / 100)
height = int(obj.shape[0] * scale_percent / 100)
dim = (width, height)
# resize image
obj = resized = cv2.resize(obj, dim, interpolation=cv2.INTER_AREA)
#create ROI
rows, cols, channels = obj.shape
bg_rows, bg_cols, bg_channels = bg.shape
roi = bg[bg_rows-rows:bg_rows:, bg_cols-cols:bg_cols:]
img2gray = cv2.cvtColor(obj, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 220, 255, cv2.THRESH_BINARY_INV)
mask_inv = cv2.bitwise_not(mask)
img1_bg = cv2.bitwise_and(roi, roi, mask=mask_inv)
img2_fg = cv2.bitwise_and(obj, obj, mask=mask)
dst = cv2.add(img1_bg, img2_fg)
bg[bg_rows-rows:bg_rows:, bg_cols-cols:bg_cols:] = dst
Image.fromarray(bg)
You should use ImageDraw() function from the pillow library. It will solve your problem by giving the mode same as the 2nd image.
I have this machine learning code for recognizing hand gestures. It uses webcam to detect hand gestures. Problem is however whenever I run this code it opens up the webcam (which works for a second) and it crashes immediately with the error ""is not responding.
Here is my opencv.py
import cv2
import numpy as np
from keras.models import load_model
from skimage.transform import resize, pyramid_reduce
model = load_model('model.h5')
while True:
cam_capture = cv2.VideoCapture(0)
_, image_frame = cam_capture.read()
# Select ROI
im2 = crop_image(image_frame, 300,300,300,300)
image_grayscale = cv2.cvtColor(im2, cv2.COLOR_BGR2GRAY)
image_grayscale_blurred = cv2.GaussianBlur(image_grayscale, (15,15), 0)
#resized_img = image_resize(image_grayscale_blurred, width = 28, height = 28, inter = cv2.INTER_AREA)
#resized_img = keras_process_image(image_grayscale_blurred)
resized_img = cv2.resize(image_grayscale_blurred,(28,28))
#ar = np.array(resized_img)
ar = resized_img.reshape(1,784)
pred_probab, pred_class = keras_predict(model, ar )
print(pred_class, pred_probab)
# Display cropped image
cv2.imshow("Image2",im2)
cv2.imshow("Image4",resized_img)
cv2.imshow("Image3",image_grayscale_blurred)
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
cam_capture.release()
cv2.destroyAllWindows()
I have deleted some redundant code. My main file is ROIinOpenCv.py
import cv2
import numpy as np
from keras.models import load_model
from skimage.transform import resize, pyramid_reduce
model = load_model('model.h5')
def get_square(image, square_size):
height, width = image.shape
if(height > width):
differ = height
else:
differ = width
differ += 4
mask = np.zeros((differ, differ), dtype = "uint8")
x_pos = int((differ - width) / 2)
y_pos = int((differ - height) / 2)
mask[y_pos: y_pos + height, x_pos: x_pos + width] = image[0: height, 0: width]
if differ / square_size > 1:
mask = pyramid_reduce(mask, differ / square_size)
else:
mask = cv2.resize(mask, (square_size, square_size), interpolation = cv2.INTER_AREA)
return mask
def keras_predict(model, image):
data = np.asarray( image, dtype="int32" )
pred_probab = model.predict(data)[0]
pred_class = list(pred_probab).index(max(pred_probab))
return max(pred_probab), pred_class
def keras_process_image(img):
image_x = 28
image_y = 28
#img = cv2.resize(img, (28,28), interpolation = cv2.INTER_AREA)
img = get_square(img, 28)
img = np.reshape(img, (image_x, image_y))
return img
def crop_image(image, x, y, width, height):
return image[y:y + height, x:x + width]
while True:
cam_capture = cv2.VideoCapture(0)
_, image_frame = cam_capture.read()
# Select ROI
im2 = crop_image(image_frame, 300,300,300,300)
image_grayscale = cv2.cvtColor(im2, cv2.COLOR_BGR2GRAY)
image_grayscale_blurred = cv2.GaussianBlur(image_grayscale, (15,15), 0)
#resized_img = image_resize(image_grayscale_blurred, width = 28, height = 28, inter = cv2.INTER_AREA)
#resized_img = keras_process_image(image_grayscale_blurred)
resized_img = cv2.resize(image_grayscale_blurred,(28,28))
#ar = np.array(resized_img)
ar = resized_img.reshape(1,784)
pred_probab, pred_class = keras_predict(model, ar )
print(pred_class, pred_probab)
# Display cropped image
cv2.imshow("Image2",im2)
cv2.imshow("Image4",resized_img)
cv2.imshow("Image3",image_grayscale_blurred)
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
cam_capture.release()
cv2.destroyAllWindows()
ps: I am using Ubuntu do you think by any chance it could be an error of granting permission to access the webcam ?
You are opening your camera in each iteration of the while loop. That is causing the error. The fix is just creating your cam_capture object outside the while loop.
Please refer below.
import cv2
import numpy as np
from keras.models import load_model
from skimage.transform import resize, pyramid_reduce
model = load_model('model.h5')
cam_capture = cv2.VideoCapture(0) # create camera object outside while-loop
while True:
_, image_frame = cam_capture.read()
# Select ROI
im2 = crop_image(image_frame, 300,300,300,300)
image_grayscale = cv2.cvtColor(im2, cv2.COLOR_BGR2GRAY)
image_grayscale_blurred = cv2.GaussianBlur(image_grayscale, (15,15), 0)
resized_img = cv2.resize(image_grayscale_blurred,(28,28))
ar = resized_img.reshape(1,784)
pred_probab, pred_class = keras_predict(model, ar )
print(pred_class, pred_probab)
# Display cropped image
cv2.imshow("Image2",im2)
cv2.imshow("Image4",resized_img)
cv2.imshow("Image3",image_grayscale_blurred)
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
cam_capture.release()
cv2.destroyAllWindows()
I'm trying to use Logitech BRIO in 3840x2160 resolution, when I execute the python code, a window opens with the camera image (in 3840x2160), but when I save a frame, the program creates a image in 1920x1080. How can I save the image in 4k
high resolution?
I'm using opencv-python==4.1.0.25
import cv2
import time
def main(args):
CAMERA_PORT = 0
IMAGEWIDTH = 3840
IMAGEHEIGHT = 2160
#Propriedades de configuracao da camera
# 3 = width da camera, 4 = height da camera
CAMERA_PROP_WIDTH = 3
CAMERA_PROP_HEIGHT = 4
camera = cv2.VideoCapture(CAMERA_PORT)
camera.set(CAMERA_PROP_WIDTH, IMAGEWIDTH)
camera.set(CAMERA_PROP_HEIGHT, IMAGEHEIGHT)
imagePath = "/home/barbosa/Documents/camera-controller/images/image.png"
while(True):
retval, image = camera.read()
cv2.imshow('Foto',image)
k = cv2.waitKey(100)
if k == 27:
break
elif k == ord('s'):
cv2.imwrite(imagePath,image)
break
cv2.destroyAllWindows()
camera.release()
return 0
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))
You can make your own custom resize function to upscale and maintain aspect ratio then save the image. I tested it on my IP camera instead of a webcam.
Here's the resize function
# Resizes a image and maintains aspect ratio
def maintain_aspect_ratio_resize(image, width=None, height=None, inter=cv2.INTER_AREA):
# Grab the image size and initialize dimensions
dim = None
(h, w) = image.shape[:2]
# Return original image if no need to resize
if width is None and height is None:
return image
# We are resizing height if width is none
if width is None:
# Calculate the ratio of the height and construct the dimensions
r = height / float(h)
dim = (int(w * r), height)
# We are resizing width if height is none
else:
# Calculate the ratio of the 0idth and construct the dimensions
r = width / float(w)
dim = (width, int(h * r))
# Return the resized image
return cv2.resize(image, dim, interpolation=inter)
Full code
import cv2
import time
# Resizes a image and maintains aspect ratio
def maintain_aspect_ratio_resize(image, width=None, height=None, inter=cv2.INTER_AREA):
# Grab the image size and initialize dimensions
dim = None
(h, w) = image.shape[:2]
# Return original image if no need to resize
if width is None and height is None:
return image
# We are resizing height if width is none
if width is None:
# Calculate the ratio of the height and construct the dimensions
r = height / float(h)
dim = (int(w * r), height)
# We are resizing width if height is none
else:
# Calculate the ratio of the 0idth and construct the dimensions
r = width / float(w)
dim = (width, int(h * r))
# Return the resized image
return cv2.resize(image, dim, interpolation=inter)
def main(args):
CAMERA_PORT = 0
IMAGEWIDTH = 3840
IMAGEHEIGHT = 2160
#Propriedades de configuracao da camera
# 3 = width da camera, 4 = height da camera
CAMERA_PROP_WIDTH = 3
CAMERA_PROP_HEIGHT = 4
camera = cv2.VideoCapture(CAMERA_PORT)
camera.set(CAMERA_PROP_WIDTH, IMAGEWIDTH)
camera.set(CAMERA_PROP_HEIGHT, IMAGEHEIGHT)
imagePath = "/home/barbosa/Documents/camera-controller/images/image.png"
while(True):
retval, image = camera.read()
cv2.imshow('Foto',image)
k = cv2.waitKey(100)
if k == 27:
break
elif k == ord('s'):
image = maintain_aspect_ratio_resize(image, width=IMAGEWIDTH)
cv2.imwrite(imagePath,image)
break
cv2.destroyAllWindows()
camera.release()
return 0
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))