yolov8 predict show=True - Iamge kills notebook kernel - python

I am testing yolov8 prediction using the following code:
from ultralytics import YOLO
# Load a model
model = YOLO("yolov8n.pt")
# Use the model
model.predict(source= "bus.jpg",show=True) # predict on an image
This works perfectly in the Spyder IDE and the resulting image can be closed by clicking the toprighthand corner in the usual way.
Using the same code in a Jupter notebook also works but the image cannot be closed. Clicking on the image gives the popup message that Python is not responding, offering the choice of closing the program or waiting. AS expected clsing the program kills the kernel.
Is there a way round this?

As this reminded me of using opencv and forgetting the cv2.destroyAllWindows a tried
cv2.waitKey(0)
cv2.destroyAllWindows()
This works!

Related

OpenCV only showing a black image when cv.imshow is used

Hi whenever I use the cv2.imshow function all I get is a black rectangle like this:
My code is here, I am using python:
from cv2 import cv2
image = cv2.imread("scren.png")
cv2.imshow("image2", image)
I have tried using different file types as well as restarting my computer. Does anyone know how to fix this? Thanks.
I'll put the answer from #fmw42 here as it solved for me too and I was looking for it for some hours
Add cv2.waitKey(0) after cv2.imshow(...).
I don't know why it solves, but solved for me.
According to the documentation the imshow function requires a subsequent command to waitKey or else the image will not show. I believe this functionality has to do with how highgui works. So to display an image in Python using opencv you will always have to do something like this:
import cv2
image = cv2.imread(r"path\to\image")
cv2.imshow("image", image)
cv2.waitKey(0)
Note about imshow
This function should be followed by a call to cv::waitKey or cv::pollKey to perform GUI housekeeping tasks that are necessary to actually show the given image and make the window respond to mouse and keyboard events. Otherwise, it won't display the image and the window might lock up. For example, waitKey(0) will display the window infinitely until any keypress (it is suitable for image display). waitKey(25) will display a frame and wait approximately 25 ms for a key press (suitable for displaying a video frame-by-frame). To remove the window, use cv::destroyWindow.
See also.

Google colab doesn't display errors, graphs, any image results while working with python and tensorflow for Image classification?

I'm using notebooks(Jupyter and google colab) but whenever there is an error, or image to be displayed as a result, or graph. When I run the code there is only empty space displayed for those image results or error, but there is no actual results displayed.
Try this
from IPython.display import Image
Image('yourimage.png')

How do I input OBS virtual cam in my python code using opencv?

I am trying to write a code for detecting the color green from a live video. I want to make a detector so that whenever the color green pops up in the screen, a counter starts counting how many times the color appears.
So for the video source, I am using the OBS Virtual Camera. But I have no idea how to input it as the source. I have seen codes inputting web cams as the source as shown below:
import numpy as np
import cv2
# Capturing video through webcam
webcam = cv2.VideoCapture(0)
Anyone have any idea how I can input the OBS virtual cam? Or does anyone know any alternative like switching to another language to do said task?
Windows will treat OBS Virtual Camera as a regular camera. The argument for cv2.VideoCapture is camera number. So up that number by 1 over and over again until the program uses the OBS Virtual Camera. And there you go.
Keep in mind that there is a bug currently reported that opencv is not parsing the stream from OBS virtual cam and just showing a black background.
https://github.com/obsproject/obs-studio/issues/3635

OpenCV face detection methods hangs

I have used OpenCVs detectMultiScale and res10_300x300_ssd_iter_140000.caffemodel.forward() methods to detect faces in the images. Exact code is shown.
Both these methods worked well providing good results until a day ago. Today, both of these processes hangs at detectMultiScale and net.forward commands respectively. Additionally when the DNN based model runs, the system memory slowly starts building up until the system hangs.
There has been no modification in any of the python libraries or system configuration in the last day. I have tried to reinstall openCV and python so far which has not been benificial.
#code for For cascade based detection:
faceCascade = cv2.CascadeClassifier('./haarcascade_frontalface_default.xml')
faces = faceCascade.detectMultiScale(frame)
#Python code for For DNN based detection:
modelFile = "res10_300x300_ssd_iter_140000.caffemodel"
configFile = "deploy.prototxt"
net = cv2.dnn.readNetFromCaffe(configFile, modelFile)
net.setInput(blob)
detections = net.forward()
I am unable to understand the reason behind the memory leak that is happening and possible solution to overcome this.

htImage display and synchronised face capture in python

I am trying to write a program for image display and face capture. I am somewhat new to python and OpenCV.
Please find the details below:
I am running Python 2.7.5 on win32 on windows XP
Open cv2 version is 3.0.0
For the program,
Images from a predefined folder needs to be displayed for a fixed time of 500 millisecond in random sequence.
The gap between the images should be covered through a black screen, which should come for any random time gap interval between 1000-1500 millisecond.
Face capture of the viewer needs to be done via webcam once image showed, in between the image show, i.e. at the point of 250 millisecond. The output of the face should be stored in another newly created folder each time the program is run.
I have written the code below, but not getting the sequence right with a synchronised face capture with Haarcascade integration(perhaps required).
I also read somewhere that 'camera index' could be involved in this with possibly the value zero assigned to it. What exactly could be its role?
Please assist in this. Thanks in advance.
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('C:\\Sourceimagepath.jpg', 1)
cv2.startWindowThread()
cv2.namedWindow("Demo")
cv2.imshow("Demo", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Categories