Load datasets and store it in another file using opencv - python-3.x

How can I read all images from datasets and store it in another location using opencv.

You can use glob to read the files in a folder.
import glob
import cv2
for file in glob.glob('source/*.png'):
img = cv2.imread(file)
filename = 'destination/'+file.split('source\\')[1]
cv2.imwrite(filename, img)
Split function of python can be used to obtain the image-name which is then written to the destination folder.
NOTE- If the folders are not in the current working directory please specify the absolute path. For more on absolute and relative paths refer here.

import os
import cv2
SOURCE_FOLDER = "a"
DESTINATION_FOLDER = "b"
for image_file_name in os.listdir(SOURCE_FOLDER):
# get full path to image file
image_path = os.path.join(SOURCE_FOLDER, image_file_name)
# read image
img = cv2.imread(image_path)
# store image in another folder
image_write_path = os.path.join(DESTINATION_FOLDER, image_file_name)
cv2.imwrite(image_write_path, img)

Related

OSError: Unable to open file (file signature not found)

I am currently doing an assignment on deep learning by downloading the assignment files from github.
import numpy as np
import matplotlib.pyplot as plt
import h5py
import scipy
from PIL import Image
from scipy import ndimage
from lr_utils import load_dataset
%matplotlib inline
You are given a dataset ("data.h5") containing: - a training set of m_train images labeled as cat (y=1) or non-cat (y=0) - a test set of m_test images labeled as cat or non-cat - each image is of shape (num_px, num_px, 3) where 3 is for the 3 channels (RGB). Thus, each image is square (height = num_px) and (width = num_px).
# Loading the data (cat/non-cat)
train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()
I ran the setup.sh file too but the error doesn't seem to go away.
lr_utils.py file:
import numpy as np
import h5py
def load_dataset():
train_dataset = h5py.File('datasets/train_catvnoncat.h5', "r")
train_set_x_orig = np.array(train_dataset["train_set_x"][:]) # your train set features
train_set_y_orig = np.array(train_dataset["train_set_y"][:]) # your train set labels
test_dataset = h5py.File('datasets/test_catvnoncat.h5', "r")
test_set_x_orig = np.array(test_dataset["test_set_x"][:]) # your test set features
test_set_y_orig = np.array(test_dataset["test_set_y"][:]) # your test set labels
classes = np.array(test_dataset["list_classes"][:]) # the list of classes
train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))
return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes
Kindly help!
I solved the issue by downloading uncorrupted .h5 files and putting them in the folder datasets/ in the same directory.
The files you downloaded are corrupted. You can visit https://github.com/abdur75648/Deep-Learning-Specialization-Coursera to download the uncorrupted files.
you can download uncorrupted files from here :
https://www.kaggle.com/datasets/muhammeddalkran/catvnoncat
and replace it in the directory of the corrupted files

Writing image into a PDF File

Trying to write an image into a pdf file at a specific location. Here in this code "Reporting.pdf" file contains a template where I have to paste my image. While running this code, the output pdf file remains the same as "Reporting.pdf" file i.e. the image doesn't get written on the pdf. Can you help me resolve this issue?
from PyPDF2 import PdfFileWriter, PdfFileReader
from reportlab.pdfgen import canvas
from io import BytesIO
import os
THIS_FOLDER = os.path.dirname(os.path.abspath(__file__))
imgPath = os.path.join(THIS_FOLDER, 'child.png')
print(imgPath)
# Using ReportLab to insert image into PDF
imgTemp = BytesIO()
imgDoc = canvas.Canvas(imgTemp)
# Draw image on Canvas and save PDF in buffer
# imgPath = "/home/sachin/Files/child-image.jpeg"
imgDoc.drawImage(imgPath, 399, 760, 160, 160) ## at (399,760) with size 160x160
imgDoc.save()
print(imgDoc)
# Use PyPDF to merge the image-PDF into the template
page = PdfFileReader("Reporting.pdf","rb").getPage(0)
overlay = PdfFileReader(BytesIO(imgTemp.getvalue())).getPage(0)
page.mergePage(overlay)
#Save the result
output = PdfFileWriter()
output.addPage(page)
pdfOutput = open('output_file101.pdf', 'wb')
output.write(pdfOutput)
pdfOutput.close()
You can't just do a drawImage with a filepath.
Consider using an ImageReader:
from reportlab.lib.utils import ImageReader
reader = ImageReader(imgPath)
imgDoc.drawImage(reader, ...)

pytesseract unable to recognize complex math formula from image

I am using pytesseract module in python, pytesseract recognizes text from image but it dosen't work on images that contain complex math formulas like under-root, derivation, integration math problem or equation.
code 2.py
# Import modules
from PIL import Image
import pytesseract
import cv2
# Include tesseract executable in your path
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
# Create an image object of PIL library
image = Image.open('23.jpg')
# img = cv2.imread('123.jpg')
# pass image into pytesseract module
# pytesseract is trained in many languages
image_to_text = pytesseract.image_to_string(image, lang='eng+equ')
image_to_text1 = pytesseract.image_to_string(image)
# Print the text
print(image_to_text)
# print(image_to_text1)
# workon digits
Output:
242/33
2x
2x+3X
2X+3x=4
2x?-3x +1=0
(x-1)(x+1) =x2-1
(x+2)/((x+3)(x-4))
7-4=3
V(x/2) =3
2xx—343=6x—3 (x#3)
Jeeta =e* +e
dy 2
S=2?-3
dz ¥
dy = (a? — 3)dx
Input image
To work with MATH language you should install the proper language for tesseract. In your case it is 'equ' from https://github.com/tesseract-ocr/tessdata/raw/3.04.00/equ.traineddata . The full list of available languages is described at https://tesseract-ocr.github.io/tessdoc/Data-Files
I'm not familiar with tesseract language install for windows. But there is a documentation at https://github.com/tesseract-ocr/tesseract/wiki :
If you want to use another language, download the appropriate training
data, unpack it using 7-zip, and copy the .traineddata file into the
'tessdata' directory, probably C:\Program Files\Tesseract-OCR\tessdata
And at first try to process your image with cli only ( without pyhton ), because cli has a full list of the options to tune.
I used this Code and it worked!
import re
import cv2
import pytesseract as tess
path = (r"C:\Users\10\AppData\Local\Tesseract-OCR\tesseract.exe")
tess.pytesseract.tesseract_cmd = path
png = "Downloads/m.png"
text = tess.image_to_string(png)
text.replace(" ", "")
pattern = re.compile("([0-9][=+-/*])")
equations = [x for x in text if bool(re.match(pattern, x))]
print(re.findall(r'(.*)', str(text))[0])

Error Opening PGM file with PIL and SKIMAGE

I have following Image file:
Image
I used PIL and Skimage to open it but I get following errors
First with PIL (tried with and without trucate option):
Code:
from PIL import Image, ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
img = Image.open("image_output.pgm")
Erorr:
OSError: cannot identify image file 'image_output.pgm'
And with Skimage:
Code:
from skimage import io
img = io.imread("image_output.pgm")
Error:
OSError: cannot identify image file <_io.BufferedReader name='image_output.pgm'>
I can open the file with GUI applications like system photo viewer and Matlab.
How can I diagnose what is wrong with image? I compared the byte data with other PGM files which I can open in Python, but could not identify the difference.
Thanks.
Your file is P2 type PGM, which means it is in ASCII - you can view it in a normal text editor. It seems neither PIL, nor skimage want to read that, but are happy to read the corresponding P5 type which is identical except it is written in binary, rather than ASCII.
There are a few options...
1) You could use OpenCV to read it:
import cv2
im = cv2.imread('a.pgm')
2) You could convert it to P5 with ImageMagick and then read the output.pgm file with skimage or PIL:
magick input.pgm output.pgm
3) If adding OpenCV, or ImageMagick as a dependency is a real pain for you, it is possible to read a PGM image yourself:
#!/usr/bin/env python3
import re
import numpy as np
# Open image file, slurp the lot
with open('input.pgm') as f:
s = f.read()
# Find anything that looks like numbers
# Technically, there could be comments that should be ignored
l=re.findall(r'[0-9P]+',s)
# List "l" will contain: P5, width, height, 255, pixel1, pixel2, pixel3...
# Technically, if l[3]>255, you should change the type of the Numpy array to uint16, but that is not the case
w, h = int(l[1]), int(l[2])
# Make Numpy image from data
ni = np.array(l[4:],dtype=np.uint8).reshape((h,w))

How to load images from Google Cloud Storage with keras.preprocessing

I am writing machine learning code that can be trained locally or in the cloud. I am using keras.preprocessing to load images, which under the hood uses PIL. It works fine for local files, but understandably doesn't understand Google Cloud Storage paths, like "gs://...".
from keras.preprocessing import image
image.load_img("gs://myapp-some-bucket/123.png")
Gives this error:
.../lib/python2.7/site-packages/keras/preprocessing/image.py", line 320, in load_img img = pil_image.open(path) File
.../lib/python2.7/site-packages/PIL/Image.py", line 2530, in open fp = builtins.open(filename, "rb") IOError: [Errno 2] No such file or directory: 'gs://myapp-some-bucket/123.png'
What is the correct way of doing this? I ultimately need a folder of images to be a single numpy array (images decoded and grayscale).
Found a replacement for keras.preprocessing.image.load_img, that understands GCS. I also included more code to read the whole folder, and turn every image in the folder into a single numpy array for training...
import os
import tensorflow as tf
from tensorflow.python.platform import gfile
filelist = gfile.ListDirectory("gs://myapp-some-bucket")
sess = tf.Session()
with sess.as_default():
x = np.array([np.array(tf.image.decode_png(tf.read_file(os.path.join(train_files_dir, filename))).eval()) for filename in filelist])
Load image:
image_path = 'gs://xxxxxxx.jpg'
image = tf.read_file(image_path)
image = tf.image.decode_jpeg(image)
image_array = sess.run(image)
Save image:
job_dir = 'gs://xxxxxxxx'
image = tf.image.encode_jpeg(image_array)
file_name = 'xxx.jpg'
write_op = tf.write_file(os.path.join(job_dir, file_name), image)
sess.run(write_op)

Resources