How to read grayscale img from a video with OpenCV? - python

I read all pictures from my pic directory and then convert them each to gray-scale with canny edge detections before writing it all to a video.
But, when I use my video software to play it, it shows a green background, and I can't read video frames from it. Could someone show me how to solve it?
Sample code
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
fourcc = cv.VideoWriter_fourcc(*"I420")
out = cv.VideoWriter("t2.avi", fourcc, 1, (640, 480), 0)
for pic in glob.glob1("./pic/", "A*"):
img = cv.imread(f"./pic/{pic}", -1)
edge = cv.Canny(img, 100, 200)
edge = cv.resize(edge, (640, 480))
out.write(edge)
out.release()
# Cant read video frame here:
cap = cv.VideoCapture("t2.avi")
ret, frame = cap.read()
if ret:
plt.imshow(frame)
else:
print("end")
cap.release()
Video plays with green background

It looks like a compatibility issue between I420 FOURCC and Grayscale format.
Replace fourcc = cv.VideoWriter_fourcc(*"I420") with:
fourcc = cv.VideoWriter_fourcc(*"GREY")
Note:
I am using OpenCV 4.5.5 in Windows 10, and it's working with "GREY".
I am not sure it's going to work in all platforms and versions.
I420 applies colored video.
You may use I420 with colored video:
Replace out = cv.VideoWriter("t2.avi", fourcc, 1, (640, 480), 0) with:
out = cv.VideoWriter("t2.avi", fourcc, 1, (640, 480), 1)
Convert edge to BGR before writing:
edge = cv.cvtColor(edge, cv.COLOR_GRAY2BGR)
out.write(edge)
Code sample using "GREY" FOURCC:
import numpy as np
import cv2 as cv
#import matplotlib.pyplot as plt
import glob
#fourcc = cv.VideoWriter_fourcc(*"I420")
fourcc = cv.VideoWriter_fourcc(*"GREY")
out = cv.VideoWriter("t2.avi", fourcc, 1, (640, 480), 0)
for pic in glob.glob1("./pic/", "A*"):
img = cv.imread(f"./pic/{pic}", -1)
edge = cv.Canny(img, 100, 200)
edge = cv.resize(edge, (640, 480))
out.write(edge)
out.release()
# Cant read video frame here:
cap = cv.VideoCapture("t2.avi")
while True:
ret, frame = cap.read()
if ret:
#plt.imshow(frame)
cv.imshow('frame', frame)
cv.waitKey(1000)
else:
print("end")
cap.release()
break
cv.destroyAllWindows()

Related

Why is python screen recorder not creating .avi file?

I'm trying to make python screen recorder that outputs AVI file. The loop should stop after pressing 'q' but that doesn't work. When I interrupt the program with Ctrl + C, it doesn't output anything either. I'm using Mac OS.
import cv2
import numpy as np
import pyautogui
SCREEN_SIZE = (1440, 900)
fourcc = cv2.VideoWriter_fourcc(*"XVID")
out = cv2.VideoWriter("output2.avi", fourcc, 20.0, (SCREEN_SIZE))
while True:
img = pyautogui.screenshot()
frame = np.array(img)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
out.write(frame)
cv2.imshow("screenshot", frame)
if cv2.waitKey(1) == ord("q"):
break
cv2.destroyAllWindows()
out.release()
I would very much appreciate your advice ;)
Here (OpenCV videowrite doesn't write video) i found the most commont problem is that it doesn't save if the resolution is wrong.
To be sure you are using the right resolution from your screen use SCREEN_SIZE = pyautogui.size()
The complete code would be:
import cv2 as cv2
import numpy as np
import pyautogui
SCREEN_SIZE = pyautogui.size() # changed this line
fourcc = cv2.VideoWriter_fourcc(*"XVID")
out = cv2.VideoWriter("output2.avi", fourcc, 20.0, SCREEN_SIZE)
while True:
img = pyautogui.screenshot()
frame = np.array(img)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
out.write(frame)
cv2.imshow("screenshot", frame)
if cv2.waitKey(1) == ord("q"):
break
cv2.destroyAllWindows()
out.release()

Canny edge output is corrupted

I am applying Canny edge detector in a video using OpenCV. I can see the result in a window and then I am writing the video. Even though I can see the right result on the window, the result in the output file is not the same.
Here is the code and what I can see in the window and in the output file. Any idea what can cause this?
I am not sure what is the output of Canny and if I have to turn it to type of data that will fit with the rest of the video.
`
import cv2
import numpy as np
cap = cv2.VideoCapture('New_video.mp4')
if (cap.isOpened() == False):
print("Unable to read camera feed")
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
fps = int(round(cap.get(5)))
out = cv2.VideoWriter('output.mp4', cv2.VideoWriter_fourcc(*'mp4v'), fps,
(frame_width, frame_height))
while (True):
ret, frame = cap.read()
if ret == False:
break
if between (cap,40000,45000):
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
frame = cv2.blur(gray,(5,5))
frame = cv2.Canny(frame,20,60)
if between (cap,45000,50000):
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
frame = cv2.blur(gray,(5,5))
frame = cv2.Canny(frame,150,250)`
out.write(frame)
cv2.imshow('frame',frame)
Try using XVID fourcc codec instead of mp4v. Also,VideoWriter method has one more parameter isColor which is set to be true by default. It should be set to False whenever we use gray scale images.
https://docs.opencv.org/3.4/dd/d9e/classcv_1_1VideoWriter.html

How to solve Opencv VideoWriter issues with global variables

I'mm writing this piece of python to display a stream of video from my webcam while at the same time record the video - which I've got working, however I've grayscaled the video streaming to my screen and time stamped it - but my recorded video is in colour! I've included the code below - I've tried using some global variables but nothing worked - any help, greatly appreciated
import cv2
import numpy as np
import time, datetime
import os
genericfilename = "recording"
filetime = str(time.time())
extension = '.avi'
filename = genericfilename + filetime +extension
frames_per_second = 100
res = '720p'
print("NEW FILE NAME: " + filename)
# Set resolution for the video capture
def change_res(cap, width, height):
cap.set(3, width)
cap.set(4, height)
# Standard Video Dimensions Sizes
STD_DIMENSIONS = {
"480p": (640, 480),
"720p": (1280, 720),
"1080p": (1920, 1080),
"4k": (3840, 2160),
}
# grab resolution dimensions and set video capture to it.
def get_dims(cap, res='1080p'):
width, height = STD_DIMENSIONS["480p"]
if res in STD_DIMENSIONS:
width,height = STD_DIMENSIONS[res]
## change the current caputre device
## to the resulting resolution
change_res(cap, width, height)
return width, height
# Video Encoding, might require additional installs
VIDEO_TYPE = {
'avi': cv2.VideoWriter_fourcc(*'XVID'),
#'mp4': cv2.VideoWriter_fourcc(*'H264'),
'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['avi']
capture = cv2.VideoCapture(0)
out = cv2.VideoWriter(filename, get_video_type(filename), 60,
get_dims(capture, res))
while(True):
ret, frame = capture.read()
out.write(frame)
grayFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
font = cv2.FONT_ITALIC = 1
cv2.putText(grayFrame, str(datetime.datetime.now()), (-330, 460), font, 3,
(200, 200, 200), 2, cv2.LINE_AA)
cv2.imshow('combilift output', grayFrame)
# Press Q on keyboard to exit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if cv2.waitKey(1) & 0xFF == ord('r'):
print(datetime.datetime.now())
capture.release()
out.release()
cv2.destroyAllWindows()
You save the frame to video, then convert frame to gray.
out.write(frame)
grayFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
If you want your recorded video to be gray, maybe reverse the order of operations and save grayFrame?
grayFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
out.write(grayFrame)
If you want to also save the texts, put the text before writing frame to output.
Lets take a look at ur code
out = cv2.VideoWriter(filename, get_video_type(filename), 60,
.....
while(True):
ret, frame = capture.read()
out.write(frame)
grayFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
You first save out then convert color
The correct sequence should be
out = cv2.VideoWriter(filename, get_video_type(filename), 60,
.....
while(True):
ret, frame = capture.read()
grayFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
out.write(grayFrame)
I don't have data to test. Just in case you experience some issue with channels. You can use opencv merge(grayFrame,grayFrame,grayFrame) to create a normal 3 channel grey scale image and save to video

Save capture part of screen using mss to video using opencv2 in python

I want to save part of screen as video. When running code below I can see the window and fps count on it (around 100-140 fps).
Code (installed mss and opencv2 are required):
import numpy as np
import cv2
import time
import mss
frame_width = 1280
frame_height = 720
frame_rate = 20.0
PATH_TO_MIDDLE = "output.avi"
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter(PATH_TO_MIDDLE, fourcc, frame_rate,
(frame_width, frame_height))
with mss.mss() as sct:
# Part of the screen to capture
monitor = {"top": 120, "left": 280, "width": 1368, "height": 770}
while "Screen capturing":
last_time = time.time()
# Get raw pixels from the screen, save it to a Numpy array
img = np.array(sct.grab(monitor))
img = cv2.resize(img, (1280, 720))
frame = img
cv2.putText(frame, "FPS: %f" % (1.0 / (time.time() - last_time)),
(10, 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
out.write(frame)
cv2.imshow('frame', frame)
# Press "q" to quit
if cv2.waitKey(25) & 0xFF == ord("q"):
break
# Clean up
out.release()
cv2.destroyAllWindows()
This code not produced any errors, so I don't understand what's wrong here. Output file output.avi created fine, but with size 5,7 KB (and this file I didn't open). I tried change VideoWriter, move out and fourcc inside the while - but didn't succeed. Also tried to change frame_rate and set frame width and height to more smaller values. Also looked at this question, but cannot succeed.
Added those two lines after frame = img helps:
frame = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
Maybe opencv can't save image as np.array so here need additional usage of cv2.cvtColor.

opencv how to use cascade on screen recorder

So I'm new to opencv and after practicing with some face detectors and understanding how to use the library, I created my own cascade and it's supposed to identify icons on my computer such as logos and others.
first to make sure my cascade worked I wrote one the detects the icons from the images I took,I took a screenshot and processed it through the cascade as an image and worked fine. the code for that is
import numpy as np
import cv2
img = cv2.imread('body.jpg')
face_csc = cv2.CascadeClassifier('new_cascade.xml')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_csc.detectMultiScale(gray, 1.1 , 4)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),3)
cv2.imshow('img',img)
cv2.waitKey(0)
after some time I wrote this for it to render my screen while detecting the icons the same way it did when I tried it on a screenshot:
import numpy as np
import cv2
from PIL import ImageGrab
fourcc = cv2.VideoWriter_fourcc(*'XVID')
face_csc = cv2.CascadeClassifier('new_cascade.xml')
out = cv2.VideoWriter("test_output.avi", fourcc, 5.0, (1366, 768))
while True:
img = ImageGrab.grab(bbox=(100, 10, 750, 750))
# convert image to numpy array
img_np = np.array(img)
# convert color space from BGR to RGB
frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
# show image on OpenCV frame
faces = face_csc.detectMultiScale(frame, 1.1 , 4)
cv2.imshow("stream", frame)
# write frame to video writer
out.write(frame)
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h), (255,0,0), 2)
roi_gray = frame[y:y+h, x:x+w]
roi_color = img_np[y:y+h,x:x+w]
if cv2.waitKey(1) == 27:
break
cv2.waitKey(0)
out.release()
but when running the code it doesn't show any errors but it also doesn't detect or identify any of the icons it just records my screen, I've tried debugging this for hours now to no avail, any ideas?
You should show and write the video after you draw rectangles, not before.
import numpy as np
import cv2
from PIL import ImageGrab
fourcc = cv2.VideoWriter_fourcc(*'XVID')
face_csc = cv2.CascadeClassifier('new_cascade.xml')
out = cv2.VideoWriter("test_output.avi", fourcc, 5.0, (1366, 768))
while True:
img = ImageGrab.grab(bbox=(100, 10, 750, 750))
# convert image to numpy array
img_np = np.array(img)
# convert color space from BGR to RGB
frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
# show image on OpenCV frame
faces = face_csc.detectMultiScale(frame, 1.1 , 4)
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h), (255,0,0), 2)
roi_gray = frame[y:y+h, x:x+w]
roi_color = img_np[y:y+h,x:x+w]
if cv2.waitKey(1) == 27:
break
cv2.imshow("stream", frame)
# write frame to video writer
out.write(frame)
cv2.waitKey(0)
out.release()

Categories