I am trying to run a pre-trained pose detection model on a new dataset.
When I pass a single image to the model, I get the correct results.
Although, when I apply a for loop for multiple images present in a folder(as shown below), openCV even fails to load half of the images.
Keep in mind I am running this code on Intel i3 dual core laptop.
import os
import cv2
DIR="C:\\Name_of_Folder\\"
for i in os.listdir(DIR):
image=cv2.imread(i)
print(image,i)
Output:
None frame_000000.jpg
None frame_000001.jpg
None frame_000002.jpg
None frame_000003.jpg
os.listdir() does not return absolute file paths, but only the file names.
import os
import cv2
DIR="C:\\Name_of_Folder\\"
for i in os.listdir(DIR):
image=cv2.imread(os.path.join(DIR, i))
print(image,i)
The path you're trying to use to read image is wrong. You have to use:
import cv2
import os
DIR="C:\\Name_of_Folder\\"
for i in os.listdir(DIR):
image=cv2.imread(os.path.join(DIR,i))
print(image,i)
Related
I have been writing a program to count the particles present in a image using the scikit-image library, and on its own it works great. The problem arises when I try to iterate such detection for all the images in a folder, to hopefully then have a average of the detected particles between all the images taken. I tried using os, but it always gave the same error:
File ~\anaconda3\lib\site-packages\imageio\core\request.py:260 in _parse_uri
raise FileNotFoundError("No such file: '%s'" % fn)
FileNotFoundError: No such file: 'C:\thesis\2022-07-18\150ms_6Hz_0000.bmp'
I have tried looking online for help, but all I could find was not helpful in my situation.
Here is the code I have for now. It is clearly very simple, but I don't have much experience with programming and this is the best I could do for now
import os
import numpy as np
from skimage import data
from skimage.io import imread
from skimage.feature import blob_dog, blob_log, blob_doh
import matplotlib.pyplot as plt
img_number = len(next(os.walk("C:/thesis/2022-07-18/500 ug/"))[2])
for images in os.listdir(r'C:\thesis\2022-07-18\50 ug'):
blob_count = imread(images)
average_blob_count = len(blob_count)/img_number
print (average_blob_count)
Thanks for the help!
I am currently working on a project using imaging flow cytometry images in python. the images are .tiff an example file name is image27_Ch1.ome.tiff . I am having a little trouble with opening these images. I have tried to use matplotlib and PIL and the tifffile library but whatever I try does not seem to work. It always tells me FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/zacha/Desktop/cell_images/27_Ch1.ome.tiff' . Although I double and triple-checked that the directory to the image is correct, even when I copy and paste the path to the image from the image properties itself it still gives me this error. I tried converting a few images into .png images and the code works and will load the images, this is not ideal because I have a data set of a few hundred thousand images. I was wondering if anyone out there in the StackOverflow universe knows how to deal with a problem like this or has dealt with .tiff images in python in the past. Below is some of the code that I have tried to open these images.
import matplotlib.pyplot as plt
path = 'C:/Users/zacha/Desktop/cell_images/27_Ch1.ome.tiff'
I = plt.imread(path)
from PIL import Image
path = 'C:/Users/zacha/Desktop/cell_images/27_Ch1.ome.tiff'
image = Image.open(path)
Thank you very much to whoever reads or answers this question.
Try rasterio and matplotlib
import rasterio
import matplotlib.pyplot as plt
src_path = "Your_sat_img.tif"
img = rasterio.open(src_path)
plt.figure(figsize=(22, 22))
plt.imshow(img.read([1,2,3]).transpose(1, 2, 0))
You can try this code to open any tiff file:
import rasterio
from rasterio.plot import show
tiff_img = rasterio.open('filename.tif')
show(tiff_img)
I am a beginner with Python, scikit-learn and numpy. I have a set of folders with images for which I want to do apply different Machine Learning algorithms. I am however struggling to get these images into numpy data that I can use.
These are my prerequisites:
Each folder name holds the key to what the images are. For example /birds/abc123.jpg and /birds/def456.jpg are both "birds"
Each image is 100x100px jpg
I am using Python 2.7
There are 2800 images in total
This is my code as far as I have gotten:
# Standard scientific Python imports
import matplotlib.pyplot as plt
# Import datasets, classifiers and performance metrics
from sklearn import svm, metrics
import numpy as np
import os # Working with files and folders
from PIL import Image # Image processing
rootdir = os.getcwd()
key_array = []
pixel_arr = np.empty((0,10000), int)
for subdir, dirs, files in os.walk('data'):
dir_name = subdir.split("/")[-1]
if "x" in dir_name:
key_array.append(dir_name)
for file in files:
if ".DS_Store" not in file:
file = os.path.join(subdir, file)
im = Image.open(file)
im_bw = im.convert('1') #Black and white
new_np = np.array(im_bw2).reshape(1,-1)
print new_np.shape
pixel_arr = np.append(pixel_arr, new_np, axis=0)
What works in this code is the browsing through the folders, getting the folder names and fetching the correct files/images. What I cannot get to work is to create a numpy array that is 2800,10000 (or maybe the correct would be 10000,2800), i.e. 2800 rows with 10000 values in each.
This solution (that I am not sure if it works) is super slow though and I am quite sure that there must be a solution that is faster and more elegant than this!
How can I create this 2800x10000 numpy array, preferrably with the index number from the key_array attached?
If you don't need all the images at the same time, you can use a generator.
def get_images():
for subdir, dirs, files in os.walk('data'):
dir_name = subdir.split("/")[-1]
if "x" in dir_name:
key_array.append(dir_name)
for file in files:
if ".DS_Store" not in file:
file = os.path.join(subdir, file)
im = Image.open(file)
im_bw = im.convert('1') #Black and white
yield np.array(im_bw2).reshape(1,-1)
This way you don't hold all the images in memory at the same time, which will probably help you out.
The use the images you would then do:
for image in get_images():
...
I'm newbie in python and in geoprocessing. I'm writing some program to calculate ndwi. To make this, I try to open geotiff dataset with gdal, but dataset can't be opened. I tried to open different tiff files (Landsat8 multiple data, Landsat7 composite, etc), but dataset is always None.
What reason to this could be? Or how can i find it out?
Here's a part of code:
import sys, os, struct
import gdal, gdalconst
from gdalconst import *
import numpy as np
from numpy import *
class GDALCalcNDWI ():
def calcNDWI(self, outFilePath):
gdal.AllRegister()
# this allows GDAL to throw Python Exceptions
gdal.UseExceptions()
filePath = "C:\\Users\\Daria\\Desktop.TIF\\170028-2007-05-21.tif"
# Open
dataset = gdal.Open(filePath, gdal.GA_ReadOnly)
# Check
if dataset is None:
print ("can't open tiff file")
sys.exit(-1)
Thanks
Whenever you have a well-known file reader that is returning None, make sure the path to your file is correct. I doubt you have a directory called Desktop.TIF, I'm assuming you just made a typo in your source code. You probably want C:\\Users\\Dara\\Desktop\\TIF\\170028-2007-05-21.tif as the path (note that Desktop.TIF ==> Desktop\\TIF).
The safest thing to do is right click on the file, go to properties, and copy/paste that path into your python source code.
I am using Python 2.7.11 and OpenCV 2.4.9. I cannot read a video by using cv2.imread() or cv2.VideoCapture().
import cv2
cap = cv2.VideoCapture('cam.avi')
print ("open = ",cap.isOpened())
OR
import cv2
cap = cv2.imread('cam.avi')
print ("open = ",cap.isOpened())
It will return false.
I don't know why. I am sure that the cam.avi is here.
imread() does not support reading from video files directly.
See also the documentation of OpenCV.
If you want to read a video with imread you will first have to convert it to single images, either via a serperate program (ffmpeg comes to mind) or using OpenCV and store the images in memory.
Try providing full path to video, like:
import cv2
cap = cv2.VideoCapture(r'C:\Users\e01069\Downloads\drop.avi')
print ("open = ",cap.isOpened())
If you run following in your same file, you would know that python is looking for your file on some different location.
import os
print os.path.abspath(__file__) #this is your current working directory
Note: .imread wouldn't work this way.