Windows 10
I reduced the code to the very core and the problem still exists.
import cv2
img = cv2.imread('imm.jpg')
cv2.imshow('image',img)
cv2.waitKey()
In this way it works as it should: it shows an image with its resolution (4k). But if I resize it in this way:
import cv2
img = cv2.imread('imm.jpg')
res = cv2.resize(img, (160,90))
cv2.imshow('image',res)
cv2.waitKey()
it creates in the toolbar a new window, whose name's image, as it should but I cannot focus it and see the result.
What's the problem?
Related
I don't know what term I should use for the window I am getting so I am attaching a screenshot of the window for reference.
I am getting this window about 7 of the 10 times I am running this code:
import cv2
import numpy as np
import face_recognition
imgElon = face_recognition.load_image_file("BasicFaceRecImg/ElonMusk2.jpg")
imgElon = cv2.cvtColor(imgElon, cv2.COLOR_BGR2RGB)
imgElon_face_loc = face_recognition.face_locations(imgElon)[0]
print(imgElon_face_loc)
imgElon_encode = face_recognition.face_encodings(imgElon)[0]
cv2.rectangle(imgElon, (imgElon_face_loc[0], imgElon_face_loc[3]), (imgElon_face_loc[1], imgElon_face_loc[2]),(255, 0, 255), 2)
cv2.imshow('Elon Musk', imgElon)
cv2.waitKey(0)
And the funny thing is that I am not getting this problem every time. It runs perfectly sometimes.
I installed opencv-python-4.2.0.32 and the problem seems to have went away. I was using version 4.3.0.36 when having the issues.
This will help. You need to resize the image sometime the OpenCV messes with an image format this will work in all OpenCV versions hope this helps
import cv2
import numpy as np
import face_recognition
# cv2.namedWindow('image', cv2.WINDOW_NORMAL)
imgElon = face_recognition.load_image_file("elon.jpg")
print(type(imgElon.shape))
a,b,c=imgElon.shape
imgElon = cv2.cvtColor(imgElon, cv2.COLOR_BGR2RGB)
imgElon_face_loc = face_recognition.face_locations(imgElon)[0]
print(imgElon_face_loc)
imgElon_encode = face_recognition.face_encodings(imgElon)[0]
cv2.rectangle(imgElon, (imgElon_face_loc[0], imgElon_face_loc[3]), (imgElon_face_loc[1], imgElon_face_loc[2]),(255, 0, 255), 2)
imgElon=cv2.resize(imgElon, (a, b))
cv2.imshow('image', imgElon)
cv2.waitKey(0)
cv2.destroyAllWindows()
I had the same problem, and was able to solve it using this code:
image = cv2.imread("./faces/test.jpg", cv2.IMREAD_COLOR)
If we look at the previews of the environments, they show the episodes increasing in the animation on the bottom right corner. https://gym.openai.com/envs/CartPole-v1/ .Is there a command to explicitly show that?
I don't think there is a command to do that directly available in OpenAI, but I've written some code that you can probably adapt to your purposes. This is the end result:
These is how I achieve the end result:
For each step, you obtain the frame with env.render(mode='rgb_array')
You convert the frame (which is a numpy array) into a PIL image
You write the episode name on top of the PIL image using utilities from PIL.ImageDraw (see the function _label_with_episode_number in the code snippet).
You save the labeled image into a list of frames.
You render the list of frames as a GIF using matplotlib utilities.
Here is the code I wrote for obtaining a GIF of the behavior of a random agent with the Episode number displayed in the top left corner of each frame:
import os
import imageio
import numpy as np
from PIL import Image
import PIL.ImageDraw as ImageDraw
import matplotlib.pyplot as plt
def _label_with_episode_number(frame, episode_num):
im = Image.fromarray(frame)
drawer = ImageDraw.Draw(im)
if np.mean(im) < 128:
text_color = (255,255,255)
else:
text_color = (0,0,0)
drawer.text((im.size[0]/20,im.size[1]/18), f'Episode: {episode_num+1}', fill=text_color)
return im
def save_random_agent_gif(env):
frames = []
for i in range(5):
state = env.reset()
for t in range(500):
action = env.action_space.sample()
frame = env.render(mode='rgb_array')
frames.append(_label_with_episode_number(frame, episode_num=i))
state, _, done, _ = env.step(action)
if done:
break
env.close()
imageio.mimwrite(os.path.join('./videos/', 'random_agent.gif'), frames, fps=60)
env = gym.make('CartPole-v1')
save_random_agent_gif(env)
You can find a working version of the code here: https://github.com/RishabhMalviya/dqn_experiments/blob/master/train_and_visualize.py#L10
import cv2
cap = cv2.VideoCapture(0)
status , photo = cap.read()
cv2.imwrite('Surendar.png',photo)
cap.release()
cv2.imshow('image', photo)
cv2.waitKey(5000)
cv2.destroyAllWindows()
I interpreted this code in my jupyter notebook. It just complies but does not show the new window of picture.
Try changing cv2.waitKey(5000) to cv2.waitKey(0) so the window will stay up until the user closes it. It looks like the window was waiting for 5000 milliseconds before it would destroy the window.
EDIT
Instead of using cv2 to display your image, try using matplot instead
import cv2
from matplotlib import pyplot as plt
cap = cv2.VideoCapture(0)
status , photo = cap.read()
cv2.imwrite('Surendar.png',photo)
cap.release()
plt.imshow(photo)
plt.show()
i can show the image using image.open, but how do i display from the binary data?
trying to use plot gets: ValueError: x and y can be no greater than 2-D, but have shapes (64,) and (64, 64, 3). this makes sense as that is what the result is supposed to be, but how do i display it?
import pathlib
import glob
from os.path import join
import matplotlib.pyplot as plt
from PIL import Image
import tensorflow as tf
def parse(image): # my like ings, but with .png instead of .jpeg.
image_string = tf.io.read_file(image)
image = tf.image.decode_png(image_string, channels=3)
image = tf.image.convert_image_dtype(image, tf.float32)
image = tf.image.resize(image, [64, 64])
return image
root = "in/flower_photos/tulips"
path = join(root,"*.jpg")
files = sorted(glob.glob(path))
file=files[0]
image = Image.open(file)
image.show()
binary=parse(file)
print(type(binary))
# how do i see this?
#plt.plot(binary) # does not seem to work
#plt.show() # does not seem to work
found a nice pillow tutorial.
from matplotlib import image
from matplotlib import pyplot
from PIL import Image
# load the image
filename='Sydney-Opera-House.jpg'
im = Image.open(filename)
# summarize some details about the image
print(im.format)
print(im.mode)
print(im.size)
# show the image
#image.show()
# load image as pixel array
data = image.imread(filename)
# summarize shape of the pixel array
print(data.dtype)
print(data.shape)
# display the array of pixels as an image
pyplot.imshow(data)
pyplot.show()
I want to analyze infrared images of solar panels and cut the panels from the images. As a first step I need to threshold the image. But if I do it with a example image from google like the one below I only get a black image. What I can do to improve?
import cv2
import numpy as np
img = cv2.imread('GooglePanelIR.png')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
ret,thresh1 = cv2.threshold(hsv[:,:,0],100,255,cv2.THRESH_BINARY)
cv2.imshow('cont imge', thresh1)
cv2.waitKey(0)
print(ret)
Since you need to threshold and its a grayscale image, import your image in grayscale.
This is what you are looking for.
import cv2
import numpy as np
img = cv2.imread('GooglePanelIR.png', 0)
ret,thresh1 = cv2.threshold(img,100,255,cv2.THRESH_BINARY)
cv2.imshow('cont imge', thresh1)
cv2.waitKey(0)
print(ret)