this should recognize the cars in the image and draw the squares around them, I don't know why it's not working... could someone give me some hints?
code and image with no detection:
IMAGE
import os
from cv2 import cv2
import matplotlib.pyplot as plt
import cvlib as cv
from cvlib.object_detection import draw_bbox
im =cv2.imread("C:\\Users\\gmobi\\PycharmProjects\\ComputerVisionStudent\\imagens\\carros.jpeg")
bbox, label, conf = cv.detect_common_objects(im)
output_image = draw_bbox(im, bbox, label, conf)
plt.imshow(output_image)
plt.show()
print('Number of cars in the image is ' + str(label.count('car')))
Installed packages:
Keras: 2.2.5
cvlib: 0.2.2
opencv-python: 4.1.1.26
tensorflow: 1.14.0
matplotlib: 3.1.1
source code from git:
https://github.com/sabiipoks/blog-posts/blob/master/Count_Number_of_Cars_in_Less_Than_10_Lines_of_Code_Using_Python.ipynb
my settings:
IMAGE
The network that cvlib is using for the object detection is YoloV3. This network expects to get an image in the RGB color space and not BGR color space which is what opencv cv2.imread is returning.
So i think you should convert the color space of the image from BGR to RGB.
im =cv2.imread("C:\\Users\\gmobi\\PycharmProjects\\ComputerVisionStudent\\imagens\\carros.jpeg")
im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
Another reason could be the size of the object in your image. The input size to Yolo is around 411x411 pixels so your image will be resized before it will be inserted to network and after the resize the objects will look to small. Try to crop the image and then insert it.
Im = im[:412,:412,:]
Related
I want to change the img_path to the frames generated from opencv:
img = image.load_img(img_path, target_size=(224, 224))
How can I rewrite it?
I am assuming that the image.load_img() function that you are using is the one from keras_utils package.
As it is noted in the documentation, load_img() accepts path to the image as the first parameter and returns:
Returns:
A PIL Image instance.
It is not mentioned in the question, but if you read the frames from the camera using opencv they should already be numpy arrays which you could to pass to your model. Of course you should resize them to (224,224) before (how to resize the image using opencv).
However, if you want to have the PIL images (to have the same type as the one returned by load_img()), you need to convert your opencv frames (numpy array) to PIL image. Follow this question and answer by #ZdaR to do this conversion:
import cv2
import numpy as np
from PIL import Image
img = cv2.imread("path/to/img.png")
# You may need to convert the color.
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
im_pil = Image.fromarray(img)
# For reversing the operation:
im_np = np.asarray(im_pil)
I have changed the format of the images to png also..but of no use. Does cv2 / imshow decrease the resolution automatically?
import numpy as np
import cv2
from matplotlib import pyplot as plt
imgL = cv2.imread('image.png',0)
imgR = cv2.imread('2.png',0)
stereo = cv2.StereoBM_create(numDisparities=16, blockSize=15)
disparity = stereo.compute(imgR,imgL)
plt.imshow(disparity, 'gray')
plt.show()
My main aim is to generate the final image with the resolution as was the supplied images.
You're using imshow from matplotlib which might be the cause of different showing behaviour.
Instead try:
cv2.imshow("Res", disparity)
cv2.waitKey(0)
cv2.destroyAllWindows()
If that is still not good, please edit the question and include the resulting image and the input image.
When running python code on the command-line, python test.py,
matplotlib doesn't show color images but rather display it as gray images.
Any workaround for displaying images when run on the command line?
demo code:
import matplotlib.pyplot as plt
import cv2
img = cv2.imread('color-image.jpg')
plt.imshow(img)
plt.show() # shows gray image
You do need to tell OpenCV that you're reading a color image, for example with this:
image = imread(argv[1], CV_LOAD_IMAGE_COLOR);
If you don't use it like that it will load the image grayscale by default.
Please change the subject as this is not Matplotlib error but OpenCV. There are plenty of examples on the OpenCV documentation:
https://docs.opencv.org/2.4/doc/tutorials/introduction/display_image/display_image.html
I want to convert an image loaded
TestPicture = cv2.imread("flowers.jpg")
I would like to run a PIL filter like on the example with the variable
TestPicture
but I'm unable to convert it back and forth between these types.
Is there a way to do these conversions?
Can OpenCV do all of the image filters that are in the PIL package?
Example:
Result:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
threshold_img = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
im_pil = cv2_to_pil(threshold_img)
pytesseract.image_to_string(im_pil)
Out[5]: 'TUM'
Yes OpenCV is more robust and flexible and can perform most of the image processing routines which are available out there, So probably this filter can be done with OpenCV> However, there may not be a straightforward API for that.
Anyways, as far as the conversion of image format from OpenCV to PIL is concerned you may use Image.fromarray as:
import cv2
import numpy as np
from PIL import Image
img = cv2.imread("path/to/img.png")
# You may need to convert the color.
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
im_pil = Image.fromarray(img)
# For reversing the operation:
im_np = np.asarray(im_pil)
But you must keep in mind that, OpenCV follows BGR convention and PIL follows RGB color convention, so to keep the things consistent you may need to do use cv2.cvtColor() before conversion.
Pillow and OpenCV use different formats of images. So you can't just read an image in Pillow and manipulate it into an OpenCV image.
Pillow uses the RGB format as #ZdaR highlighted, and OpenCV uses the BGR format. So, you need a converter to convert from one format to another.
To convert from PIL image to OpenCV use:
import cv2
import numpy as np
from PIL import Image
pil_image=Image.open("demo2.jpg") # open image using PIL
# use numpy to convert the pil_image into a numpy array
numpy_image=numpy.array(pil_img)
# convert to a openCV2 image, notice the COLOR_RGB2BGR which means that
# the color is converted from RGB to BGR format
opencv_image=cv2.cvtColor(numpy_image, cv2.COLOR_RGB2BGR)
To convert from OpenCV image to PIL image use:
import cv2
import numpy as np
from PIL import Image
opencv_image=cv2.imread("demo2.jpg") # open image using openCV2
# convert from openCV2 to PIL. Notice the COLOR_BGR2RGB which means that
# the color is converted from BGR to RGB
color_converted = cv2.cvtColor(opencv_image, cv2.COLOR_BGR2RGB)
pil_image=Image.fromarray(color_converted)
Here are two functions to convert image between PIL and OpenCV:
def toImgOpenCV(imgPIL): # Conver imgPIL to imgOpenCV
i = np.array(imgPIL) # After mapping from PIL to numpy : [R,G,B,A]
# numpy Image Channel system: [B,G,R,A]
red = i[:,:,0].copy(); i[:,:,0] = i[:,:,2].copy(); i[:,:,2] = red;
return i;
def toImgPIL(imgOpenCV): return Image.fromarray(cv2.cvtColor(imgOpenCV, cv2.COLOR_BGR2RGB));
Convert from OpenCV img to PIL img will lost transparent channel. While convert PIL img to OpenCV img will able to keep transparent channel, although cv2.imshow not display it but save as png will gave result normally.
I have a code which recognizes faces on images (dlib implementation, 68 points)
I want to rotate a bit some images but after a get following trouble: my image becomes somehow spoiled
from skimage import io
from skimage import transform as tf
img = io.imread(f)
tform = tf.SimilarityTransform(rotation=np.deg2rad(10),translation=(10,12))
img = tf.warp(img,tform)
I plot image with 2 ways:
plt.imshow(img) #the right picture (matplotlib)
win = dlib.image_window() #the left picture (dlib)
win.set_image(img) #the left picture
As you can see dlib image is broken. Also algorithm which can find facial keypoints stopps working.
Without SimilarityTransform dlib works correctly.
Help me please! I want to rotate an image and to pass it to dlib
I found the solution.
I simply needed to convert image with img_as_ubyte
from skimage import img_as_ubyte
img = img_as_ubyte(tf.warp(img,tform))