Quantcast
Channel: OpenCV Q&A Forum - RSS feed
Viewing all 2088 articles
Browse latest View live

Errors in code execution Gstreamer

$
0
0
When I run my code import cv2 filepath = raw_input("enter the path to the video ") #cap = cv2.VideoCapture(filepath) cap = cv2.VideoCapture('filesrc location-/media/kelly/Files/aaaa.mp4 ! decodebin ! appsink', cv2.CAP_GSTREAMER) if not cap.isOpened(): print("Cannot capture test src. Exiting.") quit() The following errors appear > (python:28088): GStreamer-CRITICAL **:> gst_element_make_from_uri: assertion> 'gst_uri_is_valid (uri)' failed>> (python:28088): GLib-GObject-WARNING> **: invalid cast from 'GstFileSrc' to 'GstBin'>> (python:28088): GStreamer-CRITICAL **:> gst_bin_iterate_elements: assertion> 'GST_IS_BIN (bin)' failed>> (python:28088): GStreamer-CRITICAL **:> gst_iterator_next: assertion 'it !=> NULL' failed>> (python:28088): GStreamer-CRITICAL **:> gst_iterator_free: assertion 'it !=> NULL' failed OpenCV Error: Unspecified> error (GStreamer: cannot find appsink> in manual pipeline ) in> cvCaptureFromCAM_GStreamer, file> /home/kelly/opencv-3.4.0/modules/videoio/src/cap_gstreamer.cpp,> line 805> VIDEOIO(cvCreateCapture_GStreamer> (CV_CAP_GSTREAMER_FILE, filename)):> raised OpenCV exception:>> /home/kelly/opencv-3.4.0/modules/videoio/src/cap_gstreamer.cpp:805:> error: (-2) GStreamer: cannot find> appsink in manual pipeline in> function cvCaptureFromCAM_GStreamer I tried to find a solution myself. But I did not succeed. How can fix these errors?

A bug causes crash when class inherit from cv2.KeyPoint

$
0
0
Look this code: import numpy as np import cv2 class T(cv2.KeyPoint): def __init__(self, pt): super().__init__() self.pt = pt def calculate_corners(A): A_gray = cv2.cvtColor(A, cv2.COLOR_BGR2GRAY) pa = cv2.goodFeaturesToTrack(A_gray, maxCorners=100, qualityLevel=0.01, minDistance=15) pa = np.squeeze(pa) kpa = [] for coor in pa: kpa.append(T(tuple(coor))) return kpa cap = cv2.VideoCapture("E:\\video\\test.mp4") while True: frame = cap.read()[1] if frame is None: break kpa = calculate_corners(frame) frame_corner = cv2.drawKeypoints(frame, kpa, outImage=None, color=(255, 0, 125)) cv2.imshow('frame_corner', frame_corner) cv2.waitKey(1) cv2.destroyAllWindows() cap.release() This code will crash in my system(Windows10, python3.7.3, opencv4.1.0) After test, I'm sure this is caused by class T. I guess that class T dose not inherit release moudle of cv2.KeyPoint, so it cause memory leak. It's just my conjecture. And I didn't know how to fix it. Could anyone give me some advice? Thanks a lot!

Saving a video from Tello Drone

$
0
0
Hi everyone, totally new with programming and here so bare with me. Currently, I'm using this guide to help me with my current project "https://github.com/hanyazou/TelloPy/blob/develop-0.7.0/tellopy/examples/video_effect.py" I managed to get the live video, but in order to continue I need to save that video but unfortunately I have no idea on how to do so. Then I read on this "https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_video_display/py_video_display.html" but I'm not sure where to even start. Any help would be so helpful!

I found 2 circles, I want to cut and save each circle into a different picture, how to do that in python? my code in link..

$
0
0
![image description](/upfiles/15632199052133354.jpg) [link text](https://textuploader.com/110jd)

Why i get black stripes on crops with feature matching?

$
0
0
Hi all. Im trying to make an featuring match web app for learn opencv, but i actually have problems with the crop. My code do this: - Compare two images - Find feature matchs from one to other - Warp this featured region - Crop it My results: ![image description](/upfiles/15632622061491535.jpg) How you can see i get images like that, with a black stripe on the borders. I execute the code with a bat file, with arguments, ill paste both codes: - Full code: # python libraries import rawpy import os import sys import json import argparse import cv2 import numpy as np import math from PIL import Image MIN_MATCH_COUNT = 4 HORIZONTAL = 0 VERTICAL = 1 VERTICAL_HORIZONTAL = -1 FLIPNONE = 2 RATIO = 0.75 RAN_VAL = 8.0 def calculateDistance(p0, p1): dist = math.sqrt((p1[0][0] - p0[0][0])**2 + (p1[0][1] - p0[0][1])**2) return dist def detect_matches(jpguser,raworiginal,flipmode): if flipmode == FLIPNONE: raworiginalflip = raworiginal else: raworiginalflip = cv2.flip(raworiginal,flipmode) #Detect Item detect = {} gray1 = cv2.cvtColor(jpguser, cv2.COLOR_BGR2GRAY) gray2 = cv2.cvtColor(raworiginalflip, cv2.COLOR_BGR2GRAY) minHessian = 100 sift = cv2.xfeatures2d_SURF.create(hessianThreshold=minHessian,nOctaveLayers=6) ## (3) Create flann matcher matcher = cv2.FlannBasedMatcher(dict(algorithm = 1, trees = 2), {}) ## (4) Detect keypoints and compute keypointer descriptors kpts1, descs1 = sift.detectAndCompute(gray1,None) kpts2, descs2 = sift.detectAndCompute(gray2,None) ## (5) knnMatch to get Top2 matches = matcher.knnMatch(descs1, descs2, 2) # Sort by their distance. matches = sorted(matches, key = lambda x:x[0].distance) ## (6) Ratio test, to get good matches. ratio_thresh = RATIO good = [] for m,n in matches: if m.distance < ratio_thresh * n.distance: good.append(m) detect['kpts1'] = kpts1 detect['kpts2'] = kpts2 detect['good'] = good detect['raworiginal'] = raworiginalflip detect['flipmode'] = flipmode if len(detect['good']) >= MIN_MATCH_COUNT: return detect else: if flipmode > VERTICAL_HORIZONTAL: return detect_matches(jpguser,raworiginal,flipmode-1) return detect def find_homography(item,jpguser,raworiginal,detect,matchedfile): canvas = raworiginal.copy() ## (queryIndex for the small object, trainIndex for the scene ) src_pts = np.float32([detect['kpts1'][m.queryIdx].pt for m in detect['good']]).reshape(-1, 1, 2) dst_pts = np.float32([detect['kpts2'][m.trainIdx].pt for m in detect['good']]).reshape(-1, 1, 2) ## find homography matrix in cv2.RANSAC using good match points M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, RAN_VAL) h,w = jpguser.shape[:2] pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2) dst = cv2.perspectiveTransform(pts,M) p1 = dst[0] p2 = dst[3] p3 = dst[1] p4 = dst[2] wNew = calculateDistance(p1, p2) hNew = calculateDistance(p1, p3) a = M[0,0] b = M[0,1] c = M[0,2] d = M[1,0] e = M[1,1] f = M[1,2] p = math.sqrt(a*a + b*b) r = (a*e - b*d)/(p) theta = - math.atan2(b, a) * 180 / math.pi cv2.polylines(canvas,[np.int32(dst)],True,(0,0,255),3, cv2.LINE_AA) ## (8) drawMatches matched = cv2.drawMatches(jpguser,detect['kpts1'],canvas,detect['kpts2'],detect['good'],None)#,**draw_params) ## (9) Crop the matched region from scene h,w = jpguser.shape[:2] pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2) dst = cv2.perspectiveTransform(pts,M) perspectiveM = cv2.getPerspectiveTransform(np.float32(dst),pts) found = cv2.warpPerspective(raworiginal,perspectiveM,(w,h)) ## (10) % CROP h2,w2 = raworiginal.shape[:2] item['CropH'] = (hNew/h2)*100 item['CropW'] = (wNew/w2)*100 item['FlipMode'] = detect['flipmode'] item['Rotation'] = theta item['ScaleX'] = p*100 item['ScaleY'] = r*100 ## (11) Save cv2.imwrite(item['PathImageJPGOriginal'], found) if(matchedfile): cv2.imwrite(matchedfile, matched) item['Success'] = True return item def extract_original(raworiginal, user, original, matchedfile): # JSON ITEM item = {} try: item['ImageJPGOriginal'] = original.split("\\")[-1] item['PathImageJPGOriginal'] = original item['ImageRawOriginal'] = raworiginal.split("\\")[-1] item['PathImageRawOriginal'] = raworiginal item['ImageJPGUser'] = user.split("\\")[-1] item['PathImageJPGUser'] = user item['Success'] = False if (not(matchedfile is None)): item['PathImageMatched'] = matchedfile item['ImageMatched'] = matchedfile.split("\\")[-1] if (os.path.exists(item['PathImageJPGUser']) and os.path.exists(item['PathImageRawOriginal'])): if os.path.exists(item['PathImageJPGOriginal']): os.remove(item['PathImageJPGOriginal']) ## Preparación de los datos jpguser = cv2.imread(item['PathImageJPGUser']) raworiginal = cv2.imread(item['PathImageRawOriginal']) detect = detect_matches(jpguser,raworiginal,FLIPNONE) print(len(detect['good'])) if len(detect['good']) > MIN_MATCH_COUNT: item = find_homography(item,jpguser,detect['raworiginal'],detect,matchedfile) else: item['NotMatched'] = True item['Error'] = "Not enough matches are found - {}/{}".format(len(detect['good']),MIN_MATCH_COUNT) else: item['Error'] = "File not exists" except OSError as e: item['Success'] = False item['Error'] = '%s %s %s' % (e.errno, e.strerror, e.filename) except Exception as e: item['Success'] = False item['Error'] = getattr(e, 'message', str(e)) finally: return item def main(): parser = argparse.ArgumentParser() parser.add_argument("-r", "--raworiginal", help="a path that contains the RAW Camera file") parser.add_argument("-u", "--user", help="a path where the JPGuser file") parser.add_argument("-o", "--original", help="a path where the JPGoriginal file will be copied"), parser.add_argument("-m", "--matched", help="a path where the matched file will be copied"), parser.add_argument("-json", "--json", help="a JSON string with images") args = parser.parse_args() if (not ((args.raworiginal and args.user and args.original) or args.json)): parser.print_help() if (args.raworiginal and args.user and args.original): print(json.dumps(extract_original(args.raworiginal, args.user, args.original, args.matched)), end='', flush=True) else: if (args.json): data = [] data_json = json.loads(args.json) for i in range(len(data_json)): item = extract_original(data_json[i]["PathImageRawOriginal"], data_json[i]["PathImageJPGUser"], data_json[i]["PathImageJPGOriginal"], data_json[i]["PathImageMatched"]) item["Id"] = data_json[i]["Id"] data.append(item) print(json.dumps(data), end='', flush=True) if __name__ == "__main__": main() - bat file: @echo off python.exe extract.py -r "Insert image to crop" -u "insert image for find and comare" -o "save path" -m "matched file for see matches of the result image" pause How can solve this? i want accuracy in my project but... how can do it? Actually im using python and want more accuracy process, but i dont get good results. I start thinking that warpTransform apply any rounding and for this i get this results but im newbie on opencv

I want to monitor this python process from openframeworks using C#

$
0
0
Hi, All. I have executed python process like this - $ python3 infer.py And, I want to monitor this python process from openframeworks using C#.net watchdog. When this process killed, Iit must be restarted by watchdog. Is there any watchdog Nuget Package which can monitor python? How can I get this ? Thanks in advance.

Any implementation of LineSegmentDetector for python ?

$
0
0
I see that cv2.createLineSegmentDetector implementation has been removed due original code license issues in function, any workarounds that don't include downgrading to an older version ? Thanks a lot!

CV2 installed, but Import cv2 on spyder gives an error

$
0
0
Hi, yesterday I tried different approaches to install OpenCV. when I go to the following directory: "C:\Users\Salvo\Anaconda3\Lib\site-packages\cv2" I can find the package is installed. I have windows 10, python 3.7 installed. when I open Spyder (or also Jupyter notebook) i receive the same error. > import cv2 as cv Traceback (most> recent call last):>> File> "", line> 1, in > import cv2 as cv>> File> "C:\Users\Salvo\Anaconda3\lib\site-packages\cv2\__init__.py",> line 3, in > from .cv2 import *>> ImportError: DLL load failed: he> specified module could not be found. which is weird because all the packages are there and inside the cv2 folder I found __init__.py file. Thank you for your help

CalcHist gives different results on C++ vs Python

$
0
0
I'm loading a Tiff and running calcHist in python and c++, but I can't get the results to match. Code and output attached. Thank you! **Python Implementation:** import numpy as np import cv2 as cv from PIL import Image import matplotlib.pyplot as plt im = Image.open('data.tif') img = np.array(im).astype(np.uint16) hist = cv.calcHist([img],[0],None,[256],[0,334]) print(hist) plt.hist(img.ravel(),255,range=(0,1305)); plt.show() **C++:** uint16* raster = new uint16[width*length]; for (uint16 y = 0; y < length; ++y) { for (uint16 x = 0; x < width; ++x) { raster[y*width + x] = rasterYX[y][x]; } } cv::Mat flattened(length, width, CV_16UC1, &raster[0]); cv::Mat inputHist; cv::Mat hist; float range[] = {0,334}; int num_bins = 256; const float* ranges[] = {range}; cv::calcHist(&flattened, 1, 0, cv::Mat(), hist, 1,&num_bins, ranges, true, false); std::cout << "Hist = " << std::endl << cv::format(hist, cv::Formatter::FMT_PYTHON); Here is part of the c++ output followed by the python output at the point where they begin to diverge. 0, 1, 2, 1, 4, 4, 2, 1, 11, 6, 16, 38, [0.000000e+00] [1.000000e+00] [2.000000e+00] [1.000000e+00] [5.000000e+00] [3.000000e+00] [2.000000e+00] [6.000000e+00] [6.000000e+00] [6.000000e+00] [3.100000e+01] [2.300000e+01] [3.600000e+01] [2.010000e+02] [2.160000e+02] [4.350000e+02] [9.560000e+02]

Color consistency algorithms python

$
0
0
Hey folks, I'm working on forgery detection (pixel and edge-based Color Estimation) there are some algorithms which work well. 1) gray world 2) max RGB 3) shades of gray 4) first-order Gray edge 5) Second-order Gray edge 6) Weighted Gray-Edge 7) Illuminant Map I've implemented the first 2 ( gray world, max RGB). Does anyone know OpenCV implementation of remaining algorithms? & I'm also confused between the **first-order edge and first-order grey edge** these 2 terms/algo are same?

Predict trajectory of hand based on 3D coordinates

$
0
0
Hi all, I'm looking into a way to predict the trajectory of a hand that is grabbing towards an object in Python. I already have 3D coordinates of all joints of the hand (I'm using a Charuco board and stereo vision and thus everything is calibrated, beside that I'm using a pose estimation algorithm). I'm basically just looking into an efficient way to **predict or estimate where the hand will be at frame 'x' in the future based on the 3D coordinates of the x number of previous frames**. Which method would you recommend me for this? I'm sure there has to be an 'easy way' of doing this. I already took a look into Kalman filtering, Meanshift and CamShift, but I'm not sure if these are appropriate techniques. Thanks a lot! Jérémy

getting select timeout on Linux, quirks solution doesn't work

$
0
0
Hello everyone. I'm using opencv to capture frames from a USB camera 24/7 for a face recognition project. I've noticed that I get a select timeout error during the early morning hours, when there is no people to be recognized, meaning the frames are pretty much identical (an empty room). How could I prevent this from happening. I'm using a LI-USB30-ISX017 - Leopard Imaging Inc USB 3 camera mounted on RedHat Enterprise Linux 7.5 with kernel 3.10.0-957.5.1.el7.x86_64. The ML part uses Python 3.6.5 with opencv-python 3.4.2.17 and tensorflow. I have read many a solution and none have fixed the problem: using **options uvcvideo quirks=640 nodrop=1 timeout=50000** (and 128 along with lower timeouts) has reduced the frequency of the timeouts, but they still happen occasionally. Any help would be appreciated.

VideoCapture failing for ~25% of videos from IP Camera

$
0
0
I've had a Python (3.7) app using OpenCV (3.4) for a year or so, working fine. I recently reinstalled Debian Buster, and with it upgraded to OpenCV 4.1. Somewhere in that process I've found that videos which previously worked ok now can't be read by OpenCV. A sample video with the problem is here: https://www.dropbox.com/s/tu4ddegh6yn05nu/ErrorReadingHeader.mp4?dl=0 Basically, this plays fine in most media apps, but my install of OpenCV won't open it - isOpened() returns false. However, the odd thing is that it's not a constant problem - most of the videos from the same IP camera work fine, but around a quarter can't be read. I assume there's some very minor oddity with the files, but I can't see what it is, and it's hard to get the manufacturer to do much about it because they play fine in most media apps. When I run `cv2.getBuildInformation()` I get the following: Video I/O: DC1394: NO FFMPEG: YES avcodec: YES (58.35.100) avformat: YES (58.20.100) avutil: YES (56.22.100) swscale: YES (5.3.100) avresample: NO GStreamer: YES (1.14.4) v4l/v4l2: YES (linux/videodev2.h) After turning on debugging mode (with `export OPENCV_LOG_LEVEL=debug` and `export OPENCV_VIDEOIO_DEBUG=1`) I get the following when I try to do `vc = cv2.VideoCapture('/home/Dave/ErrorReadingHeader.mp4')`: [ INFO:0] VIDEOIO: Enabled backends(5, sorted by priority): FFMPEG(1000); GSTREAMER(990); V4L2(980); CV_IMAGES(970); CV_MJPEG(960) [ WARN:0] VIDEOIO(FFMPEG): trying capture filename='/home/Dave/ErrorReadingHeader.mp4' ... [mov,mp4,m4a,3gp,3g2,mj2 @ 0x2338590] error reading header [ WARN:0] VIDEOIO(FFMPEG): can't create capture [ WARN:0] VIDEOIO(GSTREAMER): trying capture filename='/home/Dave/ErrorReadingHeader.mp4' ... [ WARN:0] VIDEOIO(GSTREAMER): can't create capture [ WARN:0] VIDEOIO(V4L2): trying capture filename='/home/Dave/ErrorReadingHeader.mp4' ... VIDIOC_REQBUFS: Inappropriate ioctl for device [ WARN:0] VIDEOIO(V4L2): can't create capture [ WARN:0] VIDEOIO(CV_IMAGES): trying capture filename='/home/Dave/ErrorReadingHeader.mp4' ... [ WARN:0] VIDEOIO(CV_IMAGES): created, isOpened=0 [ WARN:0] VIDEOIO(CV_MJPEG): trying capture filename='/home/Dave/ErrorReadingHeader.mp4' ... [ WARN:0] VIDEOIO(CV_MJPEG): can't create capture Any thoughts on how to resolve this issue? Keen to understand if this particular video can be opened in other people's installations of OpenCV 4.1, and if so what your VideoIO configuration is. Edit: for info, the following are the steps I used to install OpenCV 4.1 on Raspberry Pi 4 / Debian Buster: Temporarily increase swap file size and gpu memory: sudo sed -i 's/CONF_SWAPSIZE=100/CONF_SWAPSIZE=2048/g' /etc/dphys-swapfile sudo /etc/init.d/dphys-swapfile stop sudo /etc/init.d/dphys-swapfile start sudo sed -i '$a gpu_mem=128' /boot/config.txt Install dependencies: sudo apt-get -y install build-essential cmake pkg-config git sudo apt-get -y install libjpeg-dev libtiff-dev libjasper-dev libpng12-dev sudo apt-get -y install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev sudo apt-get -y install libgtk2.0-dev libgtk-3-dev libatlas-base-dev gfortran sudo apt-get install libcanberra-gtk* which is an ARM-specific version of GTK sudo apt install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev sudo apt-get -y install python3-dev python3-numpy python3-pip Download and prepare OpenCV: wget -O opencv.zip https://github.com/opencv/opencv/archive/4.1.0.zip wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.1.0.zip unzip opencv.zip unzip opencv_contrib.zip mv opencv-4.1.0/ opencv mv opencv_contrib-4.1.0/ opencv_contrib Build OpenCV: cd ~/opencv mkdir build cd build cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \ -D ENABLE_NEON=ON \ -D ENABLE_VFPV3=ON \ -D BUILD_TESTS=OFF \ -D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3 \ -D WITH_GSTREAMER=ON \ -D WITH_GSTREAMER_0_10=OFF \ -D OPENCV_ENABLE_NONFREE=ON \ -D INSTALL_PYTHON_EXAMPLES=OFF \ -D BUILD_EXAMPLES=OFF .. Make and install OpenCV: make -j4 noting this could take an hour or more sudo make install sudo ldconfig sudo apt-get update Copy the package for Python: sudo cp ~/opencv/build/lib/python3/cv2.cpython-37m-arm-linux-gnueabihf.so /usr/local/lib/python3.7/dist-packages/cv2.so Tidy up: cd ~ sudo sed -i 's/CONF_SWAPSIZE=2048/CONF_SWAPSIZE=100/g' /etc/dphys-swapfile sudo /etc/init.d/dphys-swapfile stop sudo /etc/init.d/dphys-swapfile start sudo sed -i 's/gpu_mem=128/gpu_mem=16/g' /boot/config.txt rm opencv.zip rm opencv_contrib.zip sudo rm -r opencv sudo rm -r opencv_contrib Test that it installed successfully: python 3, then within python import cv2, then print(cv2.__version__) Thanks a lot!

Changing gstreamer pipeline to opencv in python

$
0
0
Trying to change this gstreamer pipline to python for opencv but having a few issues gst-launch-1.0 -v playbin uri=rtsp://admin:password@192.168.1.65:554/Streaming/Channels/400 uridecodebin0::source::latency=10 This is the script I've referenced and managed to write, however I keep on getting this error import cv2 import numpy as np pipe = '"rtspsrc location=\"rtsp://admin:password@192.168.1.65:554/Streaming/Channels/400" latency=10 ! appsink' cap = cv2.VideoCapture(pipe) if not cap.isOpened(): print('VideoCapture not opened') exit(0) while True: ret, frame = cap.read() if not ret: print('empty frame') break cv2.imshow('display', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() Error: GStreamer-CRITICAL **: 09:06:55.638: gst_element_get_state: assertion 'GST_IS_ELEMENT (element)' failed

How can I get the values of the pixels that are inside a contour?

$
0
0
I'm developing an application that allows the user to load an image, make a selection of a certain region (by drawing a random closed shape) and I want to have the values of the pixels that are inside this region but I'm having trouble doing so. All the functionality of drawing the shape on top of the image I've done it with PyQt and later the image with the draw on it is passed to a function where I want to analyze it with OpenCV. Here is an example of the input image: ![image description](/upfiles/15616663139668082.png) So basically I want to get the values of the pixels inside the red shape. I know OpenCV already has a ROI function, but unfortunately the ROI selection is a square shape and I can't make square selections. They have to be a shape drawn by the user.

in here I get error for roi it is 'inconsistent use of tabs and spaces in indentation '

$
0
0
Code: import numpy as np import cv2 face_cascade = cv2.CascadeClassifier('cascades/data/haarcascade_frontalface_alt2.xml') cap = cv2.VideoCapture(0) while(True): ret,frame = cap.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, scaleFactor=1.5, minNeighbors=5) for (x, y, w, h) in faces: print(x,y,w,h) roi cv2.imshow('frame',frame) if cv2.waitKey(20) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()

4K VideoCapture very slow on Windows compared to MS Camera App

$
0
0
Hi! I have gotten a Logitech BRIO Webcam, which delivers nice smooth 30 fps of 4k images in the native Windows Camera App. Using OpenCV 4.1.0 in Python, this seems impossible. The stream is very slow at the same exposure level. Here is my code: import cv2 import time print(cv2.getBuildInformation()) time_sum = 0 frames = 0 capture = cv2.VideoCapture() capture.open(1 + cv2.CAP_DSHOW) fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G') capture.set(cv2.CAP_PROP_FOURCC, fourcc) capture.set(cv2.CAP_PROP_FPS, 30) capture.set(cv2.CAP_PROP_FRAME_WIDTH, 3840) capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 2160) while(1): before_timer = time.perf_counter() ret, frame = capture.read() if frame is None: print("Frame is empty") break else: cv2.imshow('VIDEO', frame) after_timer = time.perf_counter() - before_timer time_sum += after_timer frames += 1 if frames % 30 == 0: print("{} per second".format(frames/time_sum)) cv2.waitKey(1) Disabling the imshow does not have a significant impact on performance. Using CAP_MSMF as backend will deliver 30 FPS, but the video is just an upscaled version of a lower resolution stream, and sadly useless. I've been looking around the capture backend code for quite a while, but I can't seem to figure out where the problem lies. When disabling the RGB conversion with capture.set(cv2.CAP_PROP_CONVERT_RGB, 0), the FPS also do not improve. I also tried the same loop in C++, it was just as slow. The relevant portion of getBuildInformation() looks like this: Video I/O: DC1394: NO FFMPEG: YES (prebuilt binaries) avcodec: YES (58.35.100) avformat: YES (58.20.100) avutil: YES (56.22.100) swscale: YES (5.3.100) avresample: YES (4.0.0) GStreamer: NO DirectShow: YES Media Foundation: YES DXVA: NO Does anyone have any pointers on how to improve video performance? I am at a loss as to what to do. Hardware acceleration might be an option, but I don't know how to get that into my opencv-python package :( Setting the DEBUG environment variable does not produce any additional logging info. Thanks a lot for any help!

Read image to an already allocated Mat

$
0
0
[modules/imgcodecs/src/loadsave.cpp](https://github.com/opencv/opencv/blob/f663e8f903645a3dd66f6833f63717b86e861d77/modules/imgcodecs/src/loadsave.cpp#L400) defines a function `imread_` that allows the caller to read image directly to some already existing Mat, potentially having an already allocated memory. However, neither the `cv::imread` in the C++ API, nor `cv2.imread` in Python API [seem to allow](https://docs.opencv.org/4.1.0/d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56) reading images this way, forcing the user to allocate a new buffer every time. I'm using OpenCV version 4.1.0. If I am wrong and this is indeed possible, please let me know what should I read up on to get it to work. If not, how complex would it be to implement this functionality? I'm particularly interested in exposing it as a Python binding. What I'm trying to do is to have a master process doing IO and several worker processes to do some heavy work on the images. To cut on the inter-process communication time, I'd like to allocate some amount of shared memory, where the master would put images it reads, allowing workers to access it. I reckon it would be faster if master could store images *directly* into this shared memory, instead of allocating a new buffer, reading to it, and copying data from this buffer to shared mem. I've asked a [similar question on StackOverflow](https://stackoverflow.com/q/57161866/6919631).

[Python] Real time image stabilization with Optical Flow

$
0
0
Hi! I'm new here on this forum, and would love some help with a project I'm working on! I'm trying to make a small image stabilization programme in Python, but I can't get it to work the way I want. First, my test programme: from stabilizer import Stabilizer import cv2 import sys from imutils.video import VideoStream import time imageCapture = cv2.VideoCapture(0) imageCapture.open(0) time.sleep(2.0) frame=0 counter=0 stabilizer=Stabilizer() while True: image=imageCapture.read() frame, result=stabilizer.stabilize(image, frame) cv2.imshow("Result", result) cv2.imshow("Image", image[1]) key = cv2.waitKey(1) & 0xFF # if the `q` key was pressed, break from the loop if key == ord("q"): break counter+=1 print counter print("[INFO] cleaning up...") cv2.destroyAllWindows() imageCapture.release() ...and this is my actual stabilization programme: import numpy as np import imutils import cv2 class Stabilizer: def stabilize(self,image, old_frame): # params for ShiTomasi corner detection feature_params = dict( maxCorners = 100,qualityLevel = 0.3,minDistance = 7,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 try: if old_frame==0: ret, old_frame = image except: print("tull") old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY) p0 = cv2.goodFeaturesToTrack(old_gray, mask = None, **feature_params) ret,frame = image frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # calculate optical flow p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params) # Select good points good_new = p1[st==1] good_old = p0[st==1] # Make 3x3 matrix h=cv2.findHomography(good_old,good_new) #h=cv2.getPerspectiveTransform(good_old,good_new) #not working # Now update the previous frame and previous points #old_gray = frame_gray.copy() #p0 = good_new.reshape(-1,1,2) #cv2.destroyAllWindows() result=cv2.warpPerspective(frame,h[0], (frame.shape[1],frame.shape[0])) return frame, result This is what I thought making this: 1. Catch one frame, finding points (p0) to match. The first time the old and new frame will be the same, but the next run it should be two different frames. 2. Calculate "Optical Flow" from these points. 3. Make 3x3 transformation matrix from this "Optical Flow" 4. Apply the transformation to the image Is there any one who could help me with this one? Thanks!

I am getting an error "recognizer.train(x_train, np.array(y_labels)) TypeError: src is not a numpy array, neither a scalar"

$
0
0
program : import os from PIL import Image import numpy as np import cv2 import pickle BASE_DIR=os.path.dirname(os.path.abspath(__file__)) image_dir=os.path.join(BASE_DIR,"photos") face_cascade=cv2.CascadeClassifier('haarcascade_frontalface_default.xml') recognizer = cv2.face.LBPHFaceRecognizer_create() current_id=0 label_ids={} y_labels=[] x_train=[] for root,dirs,files in os.walk(image_dir): for file in files: if file.endswith("png") or file.endswith("jpg"): path=os.path.join(root,file) label=os.path.basename(root).replace(" ","-").lower() if not label in label_ids: label_ids[label]=current_id current_id+=1 id_=label_ids[label] y_labels.append(label) #some number x_train.append(path) #verify this image,turn into a NUMPY array,GRAY pil_image= Image.open(path).convert("L")#grayscale image_array=np.array(pil_image,"uint8") faces=face_cascade.detectMultiScale(image_array,1.5, 5) for (x,y,w,h) in faces: roi=image_array[y:y+h,x:x+w] x_train.append(roi) y_labels.append(id_) with open("labels.pickle",'wb')as f: pickle.dump(label_ids, f) recognizer.train(x_train, np.array(y_labels)) recognizer.save("trainner.yml")
Viewing all 2088 articles
Browse latest View live