Image from sqlite database used for face recognization using python - python

I have store the image in sqlite3 database using BLOB datatype and I need to extract that image for facial recognition . I have been using face recognition package to do so. The problem is i am not able to use extracted image for encoding and other operation. So, I think I have to change the datatype of the image for further processing but I am not able to find so.
The error for the code is:
RuntimeError: Unsupported image type, must be 8bit gray or RGB image.

The problem is you need to properly read the face images.
There are multiple libraries for that operation. For instance: opencv, Pillow, skimage, etc.
Here is an example of how you can read the face image:
from PIL import Image
for face in data:
face_array = Image.open(face)

Related

OpenCV: Stitch with saving warping/arrangement informations and re-stitch with the saved info

First of all, I am a beginner in computer vision field, learning OpenCV from the web.
What I am trying is stitching multispectral (bands > 3) images with OpenCV stitching APIs.
I already know that OpenCV doesn't support multispectral image.
So, the idea I came up with is as follows:
Extract the RGB images from each multispectral image.
Use cv2.Stitcher_create() and stitcher.stitch class to stitch all the RGB images (reference: https://pyimagesearch.com/2018/12/17/image-stitching-with-opencv-and-python/). And save the warping and arrangement informations (ex. Homography, matching keypoints...) in making RGB panorama.
Stitch each remaining bands' image by loading the informations that saved in step 2.
The problem is, I can't find the codes for the saving and loading informations that required in step 2 and 3.
Is the suggested method possible? And if possible, is there any tips or references that I can use?
Yes you can do it (I did it before for my paper on stitching construction plans). You need to save the camera parameters after the feature matching and probably also the seam masks.
Look here (cameras) and here (seam masks)

Combining two images horizontally in python using OpenCV

I have four images, each slices of a larger image. If I string them together horizontally, then I get the larger image. To complete this task, I'm using python 2.7 and the OpenCV library, specifically the hconcat() function. Here is the code:
with open("tempfds.jpg", 'ab+') as f:
f.write(cv2.hconcat(cv2.hconcat(cv2.imread("491411.jpg"),cv2.imread("491412.jpg")),cv2.hconcat(cv2.imread("491413.jpg"),cv2.imread("491414.jpg"))))
When I run it, everything works fine. But when I try to open the image itself, I get an error: Error interpreting JPEG image file (Not a JPEG file: starts with 0x86 0x7e). All the images I'm using are jpg's, so I don't understand why this error is occurring. Any insight is appreciated.
If you want to write a JPEG, you need:
cv2.imwrite('lovely.jpg', image)
where image is all your images concatenated together.

How can I insert EXIF/other metadata into a JPEG stored in a memory buffer?

I have created a JPEG using Python OpenCV, EXIF data being lost in the process and apparently not being able to be re-added when calling imwrite (reference: Can't keep image exif data when editing it with opencv in python).
Two questions:
In general, how can I write the original EXIF data/new custom metadata into a JPEG that exists in memory rather than a file?
Would pillow/PIL be able to maintain the EXIF data and allow supplementary metadata to be added? As of 2013 (reference: how maintain exif data of images resizes using PIL) this did not seem possible except via a tmp file (which is not an option for me).
Thanks as ever
I'm not certain I understand what you are trying to do, but I think you are trying to process an image with OpenCV and then re-insert the EXIF data you lost when OpenCV opened it...
So, hopefully you can do what you are already doing, but also open the image with PIL/Pillow and extract the EXIF data and then write it into the image processed by OpenCV.
from PIL import Image
import io
# Read your image with EXIF data using PIL/Pillow
imWithEXIF = Image.open('image.jpg')
You will now have a dict with the EXIF info in:
imWIthEXIF.info['exif']
You now want to write that EXIF data into your image you processed with OpenCV, so:
# Make memory buffer for JPEG-encoded image
buffer = io.BytesIO()
# Convert OpenCV image onto PIL Image
OpenCVImageAsPIL = Image.fromarray(OpenCVImage)
# Encode newly-created image into memory as JPEG along with EXIF from other image
OpenCVImageAsPIL.save(buffer, format='JPEG', exif=imWIthEXIF.info['exif'])
Beware... I am assuming in the code above, that OpenCVImage is a Numpy array and that you have called cvtColor(cv2.COLOR_BGR2RGB) to go to the conventional RGB channel ordering that PIL uses rather than OpenCV's BGR channel ordering.
Keywords: Python, OpenCV, PIL, Pillow, EXIF, preserve, insert, copy, transfer, image, image processing, image-processing, dict, BytesIO, memory, in-memory, buffer.

Convert grayscale png to RGB png image

I have a dataset of medical images in grayscale Png format which must be converted to RGB format. Tried many solutions but in vain.
GIMP, Menu image -> Mode -> RGB mode
If you want to just convert the format, the following method will help you:
In python3, using PILLOW and Numpy:
From PIL import Image
import numpy as np
im = Image.open(path/to/image, 'r').convert('L')
im = np.stack((im,)*3, axis=-1)
im = Image.fromarray(im)
im.save(path/to/save)
But if you want to colorize the image, know that colorization is an well-known image translation problem. Even if multiple approachs exist depending on the domain, I don't know any method that colorize any kind of images.
Some ways of doing so is to train a neural network, but for that, you need to have a dataset of B/W and colored images. Here are some approaches:
Using CNNs and considering the colorization as a regression problem: Let there be Color!
Using CNNs and considering the colorization as a classification problem: Colorful Image Colorization
Using GANs : cycle-gan

How to convert an image with PILLOW temporarily?

I know that PILLOW can convert an image from say jpg to png using the save method but is there a way to convert the image to another format and just keep it as an Image object without actually saving it as another file?
So I want to convert a user supplied image to common format for working with in the program because certain tools I am using only support png.
jpg and png are just compression techniques for saving an image to a file. An image as an object, is just an array of RGB(or any other colorspace/format used by the library which you used to read the file) values of all the pixels.
So technically, you can use the image object as the common format for working with other tools. But you need to keep in mind about the colorspace which is used by each library. Like, OpenCV considers an image object in BGR format, so you need to convert the image object to this format before you use it in OpenCV.

Categories

Resources