Reading Multiple Images One at a Time - python

I have multiple images stored at my desktop that need to be processed one at a time by Tensorflow. My problem is that I don't know how to create a loop to accomplish the individual reading and processing of images.
I found on this site the code that enables the reading of multiple images stored locally. I placed the code where I thought I would work but it didn't.
The results obtained by the code below, out of thirty images only the first two were shown. Sorry about the formatting. Not an expert. I think the loop shouldn't be placed there as a whole and the indentation has to do something with the bad results. Any tips will be highly appreciated.
Thanks
...code
from PIL import Image
import os, sys
path = 'C:\\Users\\Owner\\Desktop\\Images\\'
dirs = os.listdir( path )
....Code
if __name__ == '__main__':
...code
for item in dirs:
if os.path.isfile(path+item):
im = Image.open(path+item)
f, e = os.path.splitext(path+item)
loadedImage = path + item
parser.add_argument('--image', type=str, default='loadedImage')
....code
for i, single_3d in enumerate(pose_3d):
plot_pose(single_3d)
pass
I switched the above code to and it worked. However, my images are nor displayed sequentially. Can anyone tell me how to fix this?:
This is the code:
import argparse
import logging
import time
import os
import ast
import common
import cv2
import numpy as np
from estimator import TfPoseEstimator
from networks import get_graph_path, model_wh
import sys
from PIL import Image
path = 'C:\\Users\\Owner\\Desktop\\data\\'
dirs = os.listdir(path)
dirs.sort()
from lifting.prob_model import Prob3dPose
from lifting.draw import plot_pose
logger = logging.getLogger('TfPoseEstimator')
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('[%(asctime)s] [%(name)s] [%(levelname)s] %
(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
if __name__ == '__main__':
os.chdir('..')
for item in dirs:
im = Image.open(path+item)
f, e = os.path.splitext(path+item)
parser = argparse.ArgumentParser(description='tf-pose-estimation run')
nameimage = f + e
print(nameimage)
parser.add_argument('--image', type=str, default = nameimage)
parser.add_argument('--model', type=str,
default='mobilenet_thin_432x368', help='cmu_640x480 / cmu_640x360 /
mobilenet_thin_432x368')
parser.add_argument('--scales', type=str, default='[1.0, (1.1, 0.05)]', help='for multiple scales, eg. [1.0, (1.1, 0.05)]')
args = parser.parse_args()
scales = ast.literal_eval(args.scales)
w, h = model_wh(args.model)
e = TfPoseEstimator(get_graph_path(args.model), target_size=(w, h))
image = common.read_imgfile(args.image, None, None)
t = time.time()
humans = e.inference(image, scales=[None])
elapsed = time.time() - t
logger.info('inference image: %s in %.4f seconds.' % (args.image, elapsed))
image = cv2.imread(args.image, cv2.IMREAD_COLOR)
image = TfPoseEstimator.draw_humans(image, humans, imgcopy=False)
cv2.imshow('tf-pose-estimation result', image)
cv2.waitKey()
logger.info('3d lifting initialization.')
poseLifting = Prob3dPose('./src/lifting/models/prob_model_params.mat')
image_h, image_w = image.shape[:2]
standard_w = 640
standard_h = 480
pose_2d_mpiis = []
visibilities = []
for human in humans:
pose_2d_mpii, visibility = common.MPIIPart.from_coco(human)
pose_2d_mpiis.append([(int(x * standard_w + 0.5), int(y * standard_h + 0.5)) for x, y in pose_2d_mpii])
visibilities.append(visibility)
pose_2d_mpiis = np.array(pose_2d_mpiis)
visibilities = np.array(visibilities)
transformed_pose2d, weights = poseLifting.transform_joints(pose_2d_mpiis, visibilities)
pose_3d = poseLifting.compute_3d(transformed_pose2d, weights)
pose_3dqt = np.array(pose_3d[0]).transpose()
for point in pose_3dqt:
#my points print(point)
import matplotlib.pyplot as plt
fig = plt.figure()
a = fig.add_subplot(2, 2, 1)
a.set_title('Result')
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
a = fig.add_subplot(2, 2, 2)
tmp = np.amax(e.heatMat, axis=2)
plt.imshow(tmp, cmap=plt.cm.gray, alpha=0.5)
plt.colorbar()
tmp2 = e.pafMat.transpose((2, 0, 1))
tmp2_odd = np.amax(np.absolute(tmp2[::2, :, :]), axis=0)
tmp2_even = np.amax(np.absolute(tmp2[1::2, :, :]), axis=0)
a = fig.add_subplot(2, 2, 3)
a.set_title('Vectormap-x')
plt.imshow(tmp2_odd, cmap=plt.cm.gray, alpha=0.5)
plt.colorbar()
a = fig.add_subplot(2, 2, 4)
a.set_title('Vectormap-y')
plt.imshow(tmp2_even, cmap=plt.cm.gray, alpha=0.5)
plt.colorbar()
for i, single_3d in enumerate(pose_3d):
plot_pose(single_3d)
plt.show()
pass
Image 1 Jumps to image 10.

The answer from this question may provide some example how to read the image in the folder.
My jpg pictures are stored sequentially pic0,pic1,pic2,pic3,pic4,pic20,pic30,pic100 but my code displays the images pic0,pic1,pic100,pic2,pic20,pic3,pic30.... How do I avoid that.?
The issue is with the way the name in the image dataset, and additionally with the sorting step you add after reading the directory list. My suggestion is to rename the file image to have zero leading (example pic000, pic001, .., pic010, pic011, ..).
To rename the files (given your image names), a minimal example:
import os
s1 = os.listdir('.')
for s in s1:
if ".jpg" not in s:
continue
if len(s)==8: # handle pic1.jpg pic2.jpg
#print(s[:-5] + '00' + s[3] + '.jpg')
os.rename(s, s[:-5] + '00' + s[3] + '.jpg')
elif len(s)==9: # handle pic10.jpg pic11.jpg
os.rename(s, s[:-6] + '0' + s[3:5] + '.jpg')

Related

Coordinates extracted from mediapipe are always same

I am trying to extract coordinates of all features from the mediapipe library using face mesh but for every image that I am testing it's giving the same coordinates. I don't understand what's wrong here. If anyone could help, it would be great!
import mediapipe as mp
import cv2
import matplotlib.pyplot as plt
# from mpl_toolkits.mplot3d import Axes3D
import json
import os
from os import path
file_name = "./json_output"
img_base = cv2.imread("./johnny-depp-sunglasses-hat-smile-wallpaper.jpg")
img = img_base.copy()
mp_face_mesh = mp.solutions.face_mesh
face_mesh = mp_face_mesh.FaceMesh(static_image_mode=True)
results = face_mesh.process(img)
landmarks = results.multi_face_landmarks[0]
xs, ys, zs = [], [], []
CONTOUR_LIST = ["FACEMESH_LIPS", "FACEMESH_FACE_OVAL", "FACEMESH_LEFT_IRIS",
"FACEMESH_LEFT_EYEBROW","FACEMESH_LEFT_EYE", "FACEMESH_RIGHT_IRIS",
"FACEMESH_RIGHT_EYEBROW", "FACEMESH_RIGHT_EYE"]
This is the main function:
def extract_landmarks(inp):
img = img_base.copy()
for landmark in landmarks.landmark:
x = landmark.x
y = landmark.y
z = landmark.z
xs.append(x)
ys.append(y)
zs.append(z)
relative_x = int(x * img_base.shape[1])
relative_y = int(y * img_base.shape[0])
cv2.circle(img, (relative_x, relative_y),
radius=5, color=(0,0,255),
thickness=-1)
# fig = plt.figure(figsize=(15,15))
# plt.imshow(img[:,:,::-1])
# plt.show()
img = img_base.copy()
for i in inp:
for src_id, tar_id in i:
source = landmarks.landmark[src_id]
target = landmarks.landmark[tar_id]
relative_source = int(source.x * img.shape[1]), int(source.y * img.shape[0])
relative_target = int(target.x * img.shape[1]), int(target.y * img.shape[0])
cv2.line(img, relative_source, relative_target,
color=(255,255,255), thickness=2)
fig = plt.figure(figsize=(15,15))
plt.imshow(img[:,:,::-1])
plt.show()
result = inp
# print(result)
my_json = list(result)
# my_ans = [{f"{CONTOUR_LIST[k]}":{'x':x, 'y':y}} for k in range(len(CONTOUR_LIST)) for i in my_json for x,y in i]
my_ans = [{f"{CONTOUR_LIST[k]}":{'x':x, 'y':y}} for k in range(0, 8) for i in my_json for x,y in i]
#
# print(my_ans, sep="\n", end="\n")
# print("\n")
# print("\n")
# coordinates.append(my_ans)
# print(my_ans, end="\n", sep="\n")
if os.path.exists(file_name):
print("Already exists!")
# with open(file_name, 'w') as f:
# f.write(json.dumps(my_ans, indent=4, separators=(',',': ')))
else:
with open(file_name, 'w') as file:
json.dump(my_ans, file,
indent=4,
separators=(',',': '))
return len(my_json)
And this is the code for calling the function:
features = []
features.append(mp_face_mesh.FACEMESH_LIPS)
features.append(mp_face_mesh.FACEMESH_FACE_OVAL)
features.append(mp_face_mesh.FACEMESH_LEFT_IRIS)
features.append(mp_face_mesh.FACEMESH_LEFT_EYEBROW)
features.append(mp_face_mesh.FACEMESH_LEFT_EYE)
features.append(mp_face_mesh.FACEMESH_RIGHT_IRIS)
features.append(mp_face_mesh.FACEMESH_RIGHT_EYEBROW)
features.append(mp_face_mesh.FACEMESH_RIGHT_EYE)
extract_landmarks(features)
For every image, I am getting the same coordinates.
The coordinates in json_output is fixed, look at the face mesh.
The content of json_output is the numbers in the image.
relative_x and relative_y are the coordinates relative to the width and
height of the picture.

how to append float numbers to a list in python

I am trying to compare one image with all images of another file , get the difference percentage and print file name of the least difference percentage .... if i try to append the output differences to a list ... i get an error saying " float values cannot be iterated".... this is what i have done so far ....
from itertools import izip
import os
import numpy as np
import cv2
from matplotlib import pyplot as plt
from PIL import Image
import math
res = 0
def take_and_save_picture(im_save):
'''Take a picture and save it
Args:
im_save: filepath where the image should be stored
'''
camera_port = 0
ramp_frames = 30
cap = cv2.VideoCapture(camera_port)
def get_image():
retval, im = cap.read()
return im
for i in xrange(ramp_frames):
temp = get_image()
print("Taking image...")
# Take the actual image we want to keep
camera_capture = get_image()
#im_save_tmp = im_save + '.jpg'
im_save_tmp = im_save
# A nice feature of the imwrite method is that it will automatically choose the
# correct format based on the file extension you provide. Convenient!
cv2.imwrite(im_save_tmp, camera_capture)
# You'll want to release the camera, otherwise you won't be able to create a new
# capture object until your script exits
# del(cap)
img1 = cv2.imread(im_save_tmp, 0)
edges = cv2.Canny(img1, 100, 200)
cv2.imwrite(im_save, edges)
cv2.waitKey(0)
cv2.destroyAllWindows()
def re(path1,path2):
#path1 = raw_input("Enter the path1:")
#path2 = raw_input("Enter the path2:")
i2= Image.open(path2)
listing = os.listdir(path1)
for file in listing:
i1 = Image.open(path1 + file)
assert i1.mode == i2.mode, "Different kinds of images."
assert i1.size == i2.size, "Different sizes."
pairs = izip(i1.getdata(), i2.getdata())
if len(i1.getbands()) == 1:
# for gray-scale jpegs
dif = sum(abs(p1-p2) for p1,p2 in pairs)
else:
dif = sum(abs(c1-c2) for p1,p2 in pairs for c1,c2 in zip(p1,p2))
ncomponents = i1.size[0] * i1.size[1] * 3
res = (dif / 255.0 * 100) / ncomponents
print "Difference (percentage):", res
def main():
capture_img = "/Users/Me/Documents/python programs/New/pro.png"
#img_to_compare = "/Users/Me/Documents/python programs/compare/img2.jpg"
take_and_save_picture(capture_img)
path1 = "/Users/Me/Documents/python programs/New/numbers1/"
path2 = "/Users/Me/Documents/python programs/New/pro.png"
re(path1,path2)
if __name__ == '__main__':
main()
the output is the difference
Difference (percentage): 2.52484809028
Difference (percentage): 2.64822048611
Difference (percentage): 2.64822048611
Difference (percentage): 3.55436197917
the values that i get in "res" have to be stored in a list and the minimum value should be found and printed.... please give me some code ... totally new to python ... thank you ...
You're code must be like this:
#######
list_dif = []
def re(path1,path2):
#path1 = raw_input("Enter the path1:")
#path2 = raw_input("Enter the path2:")
i2= Image.open(path2)
listing = os.listdir(path1)
for file in listing:
i1 = Image.open(path1 + file)
assert i1.mode == i2.mode, "Different kinds of images."
assert i1.size == i2.size, "Different sizes."
pairs = izip(i1.getdata(), i2.getdata())
if len(i1.getbands()) == 1:
# for gray-scale jpegs
dif = sum(abs(p1-p2) for p1,p2 in pairs)
else:
dif = sum(abs(c1-c2) for p1,p2 in pairs for c1,c2 in zip(p1,p2))
ncomponents = i1.size[0] * i1.size[1] * 3
#######
for n in range(ncomponents):
res = (dif / 255.0 * 100) / (ncomponents + 1)
list_dif.append(res)
print "Difference (percentage):", list_dif
Something like this?
def re(path1,path2):
#path1 = raw_input("Enter the path1:")
#path2 = raw_input("Enter the path2:")
i2= Image.open(path2)
listing = os.listdir(path1)
res = []
for file in listing:
i1 = Image.open(path1 + file)
assert i1.mode == i2.mode, "Different kinds of images."
assert i1.size == i2.size, "Different sizes."
pairs = izip(i1.getdata(), i2.getdata())
if len(i1.getbands()) == 1:
# for gray-scale jpegs
dif = sum(abs(p1-p2) for p1,p2 in pairs)
else:
dif = sum(abs(c1-c2) for p1,p2 in pairs for c1,c2 in zip(p1,p2))
ncomponents = i1.size[0] * i1.size[1] * 3
res.append((dif / 255.0 * 100) / ncomponents)
print "Difference (percentage):", res
minimum = min(res) # Find minimum value in res
print(minimum)

'cv2.face_BasicFaceRecognizer' object has no attribute 'getParams' Python

I'm having some trouble creating a face recognition system with OpenCV and Python. I was trying to use the documentation given by Philipp Wagner, and I have the following code:
import os
import sys
import cv2
import numpy as np
def normalize(X, low, high, dtype=None):
"""Normalizes a given array in X to a value between low and high."""
X = np.asarray(X)
minX, maxX = np.min(X), np.max(X)
# normalize to [0...1].
X = X - float(minX)
X = X / float((maxX - minX))
# scale to [low...high].
X = X * (high-low)
X = X + low
if dtype is None:
return np.asarray(X)
return np.asarray(X, dtype=dtype)
def read_images(path, sz=None):
"""Reads the images in a given folder, resizes images on the fly if size is given.
Args:
path: Path to a folder with subfolders representing the subjects (persons).
sz: A tuple with the size Resizes
Returns:
A list [X,y]
X: The images, which is a Python list of numpy arrays.
y: The corresponding labels (the unique number of the subject, person) in a Python list.
"""
c = 0
X,y = [], []
for dirname, dirnames, filenames in os.walk(path):
for subdirname in dirnames:
subject_path = os.path.join(dirname, subdirname)
for filename in os.listdir(subject_path):
try:
im = cv2.imread(os.path.join(subject_path, filename), cv2.IMREAD_GRAYSCALE)
# resize to given size (if given)
if (sz is not None):
im = cv2.resize(im, sz)
X.append(np.asarray(im, dtype=np.uint8))
y.append(c)
except IOError, (errno, strerror):
print "I/O error({0}): {1}".format(errno, strerror)
except:
print "Unexpected error:", sys.exc_info()[0]
raise
c = c+1
return [X,y]
if __name__ == "__main__":
out_dir = None
if len(sys.argv) < 2:
print "USAGE: facerec_demo.py </path/to/images> [</path/to/store/images/at>]"
sys.exit()
[X,y] = read_images(sys.argv[1])
y = np.asarray(y, dtype=np.int32)
# If a out_dir is given, set it:
if len(sys.argv) == 3:
out_dir = sys.argv[2]
model = cv2.face.createEigenFaceRecognizer()
model.train(np.asarray(X), np.asarray(y))
model.save('individual.xml')
[p_label, p_confidence] = model.predict(np.asarray(X[0]))
# Print it:
print "Predicted label = %d (confidence=%.2f)" % (p_label, p_confidence)
print model.getParams()
# Now let's get some data:
mean = model.getMat("mean")
eigenvectors = model.getMat("eigenvectors")
# We'll save the mean, by first normalizing it:
mean_norm = normalize(mean, 0, 255, dtype=np.uint8)
mean_resized = mean_norm.reshape(X[0].shape)
if out_dir is None:
cv2.imshow("mean", mean_resized)
else:
cv2.imwrite("%s/mean.png" % (out_dir), mean_resized)
for i in xrange(min(len(X), 16)):
eigenvector_i = eigenvectors[:,i].reshape(X[0].shape)
eigenvector_i_norm = normalize(eigenvector_i, 0, 255, dtype=np.uint8)
if out_dir is None:
cv2.imshow("%s/eigenface_%d" % (out_dir,i), eigenvector_i_norm)
else:
cv2.imwrite("%s/eigenface_%d.png" % (out_dir,i), eigenvector_i_norm)
if out_dir is None:
cv2.waitKey(0)
But it keeps me getting the following error:
print model.getParams()
AttributeError: 'cv2.face_BasicFaceRecognizer' object has no attribute 'getParams'
Any idea why I can't get the any parameters? I thought that maybe it is because of the incorporation of the submodule cv2.face,and therefore it might be some alternative to model.getParams() as well as getMat() but I'm just guessing...
Thanks in advance.
Maybe it's too late but this what I did.
First, to see the list of methods that supports your cv2.face
model = cv2.face.createEigenFaceRecognizer ()
help (model)
And as you'll notice, there are some changes no longer used: model.getMat ("mean") now only used mean = model.getMean().
I hope it helps you :)

Saving 3D+Time (XYCZT) stacks with Bio-Formats on Python

I'm having some trouble to save 3D+Time Tiff files, from Numpy arrays in Python, using the Bioformats standard.
For a start, I have a numpy array with 5 dimensions, ordered XYCZT, in my case (267, 518, 1, 331, 3).
Here is the function I have so far:
# Write file to disk
def write(img_XYCZT, path, type='uint16', verbose=True):
import bioformats.omexml as ome
import javabridge as jutil
import bioformats
import numpy as np
import os
import sys
if verbose:
print('Dimensions (XYCZT): ' + str(np.shape(img_XYCZT)))
sys.stdout.flush()
# Get the new dimensions
SizeX = np.shape(img_XYCZT)[0]
SizeY = np.shape(img_XYCZT)[1]
SizeC = np.shape(img_XYCZT)[2]
SizeZ = np.shape(img_XYCZT)[3]
SizeT = np.shape(img_XYCZT)[4]
# Start JVM for bioformats
jutil.start_vm(class_path=bioformats.JARS)
# Getting metadata info
omexml = ome.OMEXML()
omexml.image(0).Name = os.path.split(path)[1]
p = omexml.image(0).Pixels
assert isinstance(p, ome.OMEXML.Pixels)
p.SizeX = SizeX
p.SizeY = SizeY
p.SizeC = SizeC
p.SizeT = SizeT
p.SizeZ = SizeZ
p.DimensionOrder = ome.DO_XYCZT
p.PixelType = type
p.channel_count = SizeC
p.plane_count = SizeZ
p.Channel(0).SamplesPerPixel = SizeC
omexml.structured_annotations.add_original_metadata(ome.OM_SAMPLES_PER_PIXEL, str(SizeC))
# Converting to omexml
xml = omexml.to_xml()
# Write file using Bioformats
if verbose:
print ('Writing frames:'),
sys.stdout.flush()
for frame in range(SizeT):
if verbose:
print('[' + str(frame + 1) + ']'),
sys.stdout.flush()
index = frame
pixel_buffer = bioformats.formatwriter.convert_pixels_to_buffer(img_XYCZT[:, :, :, :, frame], type)
script = """
importClass(Packages.loci.formats.services.OMEXMLService,
Packages.loci.common.services.ServiceFactory,
Packages.loci.formats.out.TiffWriter);
var service = new ServiceFactory().getInstance(OMEXMLService);
var metadata = service.createOMEXMLMetadata(xml);
var writer = new TiffWriter();
writer.setBigTiff(true);
writer.setMetadataRetrieve(metadata);
writer.setId(path);
writer.setInterleaved(true);
writer.saveBytes(index, buffer);
writer.close();
"""
jutil.run_script(script, dict(path=path, xml=xml, index=index, buffer=pixel_buffer))
if verbose:
print ('[Done]')
sys.stdout.flush()
if verbose:
print('File saved on ' + str(path))
sys.stdout.flush()
Being img_XYCZT the numpy array, and path the place to save the file. Probably the function uses lots of redundancy for the metadata, but thats me fighting for it to work somehow...
Checking the saved file on Fiji, the Z information is as C channels:
The file simply doesn't have the Z dimension... I've been struggling with this for some time, any help is highly appreciated !
Thanks

downsampling images as a function in Python

I am trying to resample some tiff files from 2000*2000 to 500*500.
I have created a function and I tried for one file and it worked nicely. Now I want to apply it for all the available file I have.
I want to write the output of the function and I have written the code based on my knowledge and I receive error on the writing out_file. I have copied the both function and main code for your consideration. The main code just read the tif files according to their naming and applies the function. I would be thankful if sb could guide me where my mistake is.
#*********function********************
def ResampleImage(infile):
fp = open(infile, "rb")
p = ImageFile.Parser()
while 1:
s = fp.read()
if not s:
break
p.feed(s)
img = p.close()
basewidth = 500
wpercent = (basewidth / float(img.size[0]))
hsize = int((float(img.size[1]) * float(wpercent)))
outfile=img.resize((basewidth, hsize), PIL.Image.ANTIALIAS)
return outfile
#********* main code********
import os,sys
import ImageResizeF
import PIL
from PIL import Image
from PIL import Image,ImageFile
tpath = 'e:/.../resampling_test/all_tiles/'
tifext = '.tif'
east_start = 32511616
north_start = 5400756
ilist = range (0,14)
jlist = range (0,11)
north = north_start
ee = ',4_'
en = ',2'
for i in ilist:
east = east_start
north = north_start + i * 400
snorth = str (north)
for j in jlist:
east = east_start + j * 400
seast = str (east)
infile = tpath + seast + ee + snorth + en + tifext
output = tpath + seast + ee + snorth + en + '_res'+tifext
out_file = ImageResizeF.ResampleImage(infile)
out_file.write (output)
out_file.close ()
Your error is probably related to what you are returning from ImageResizeF.ResampleImage, is it a file handle? Otherwise you are doing it wrong because you cannot close() something which is not a file handle. You should do the whole file processing inside the function or return an image object, for example:
def process_image(image):
"Processes the image"
image.resize((x, y), Image.ANTIALIAS) # or whatever you are doing to the image
return image
image = Image.open('infile.tiff')
proc_image = process_image(image)
proc_image.save('outfile.tiff')

Categories