i am trying to read my own images which are 28 x 28 dimension: the images are stored in a folder called, my_own_images. and the image name is 2828_my_own_3.png, 2828_my_own_7.png etc...
i am using the imagio.imread(image_file_name, as_gray = True). However i get an error for the as_gray. I am trying to convert them into grey scale
****THE CODE IS BELOW****
*import imageio
import glob
import numpy
import matplotlib.pyplot as plt
%matplotlib inline
my_dataset = []
for image_file_name in glob.glob('my_own_images/2828_my_own_?.png'):
print ("loading ... ", image_file_name)
# use the filename to set the correct label
label = int(image_file_name[-5:-4])
# load image data from png files into an array
img_array = imageio.imread(image_file_name)
print(img_array.shape)
# reshape from 28x28 to list of 784 values, invert values
img_data = 255.0 - img_array.reshape(784)
# then scale data to range from 0.01 to 1.0
img_data = (img_data / 255.0 * 0.99) + 0.01
print(numpy.min(img_data))
print(numpy.max(img_data))
# append label and image data to test data set
record = numpy.append(label,img_data)
print(record)
my_dataset.append(record)
pass*
The ERROR im getting:
open() got an unexpected keyword argument 'as_gray'
It seems we are reading the same book (Make Your Own Neural Network), I have encountered the same error with anaconda (I have imageio 2.2.0 Install), so I updated the imageio to 2.3.0, re-launch jupyter Notebook, re-run the code again it work for me. (hope it help you)
Related
I have a multiframe tiff image with mode 1 that I want to convert to a multiframe tiff with mode 'RGB'. It is only saving a single frame in the output. Am I missing something?
def q(file_path='test.tiff'):
with Image.open(file_path) as image:
if image.mode != 'RGB':
n = image.convert('RGB')
n.save(fp='new.tiff', format="TIFF", save_all=True, compression="None")
return
You need to iterate over the sequence of frames. See here.
Code attributable to above link:
from PIL import Image
with Image.open("animation.gif") as im:
im.seek(1) # skip to the second frame
try:
while 1:
im.seek(im.tell() + 1)
# do something to im
except EOFError:
pass # end of sequence
I am trying to evaluate a Raspberry Pi performance with a Google Goral Edge TPU USB device and without it for an image classification task on a video file. I have managed to evaluate the peformance using the Edge TPU USB device already. However, when I try running a tensorflow lite code to run inference it gets me an error that tells me I need to plugin the device:
ValueError: Failed to load delegate from libedgetpu.so.1
What I am doing specifically is running inference on a video using the coral device and saving every frame in the video to benchmark the hardware.
import argparse
import time
import cv2
import numpy as np
from pycoral.adapters import classify, common
from pycoral.utils.dataset import read_label_file
from pycoral.utils.edgetpu import make_interpreter
from utils import visualization as visual
WINDOW_NAME = "Edge TPU Image classification"
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--model", help="File path of Tflite model.", required=True)
parser.add_argument("--label", help="File path of label file.", required=True)
parser.add_argument("--top_k", help="keep top k candidates.", default=2, type=int)
parser.add_argument("--threshold", help="Score threshold.", default=0.0, type=float)
parser.add_argument("--width", help="Resolution width.", default=640, type=int)
parser.add_argument("--height", help="Resolution height.", default=480, type=int)
parser.add_argument("--videopath", help="File path of Videofile.", default="")
args = parser.parse_args()
# Initialize window.
cv2.namedWindow(WINDOW_NAME)
cv2.moveWindow(WINDOW_NAME, 100, 200)
# Initialize engine and load labels.
count = 0
interpreter = make_interpreter(args.model)
interpreter.allocate_tensors()
labels = read_label_file(args.label) if args.label else None
elapsed_list = []
cap = cv2.VideoCapture('/home/pi/coral-usb/pycoral/test_data/video.mkv)
while cap.isOpened():
_, frame = cap.read()
im = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
cv2.imwrite("/home/pi/Desktop/frames/frame_%d.jpeg" % count, frame)
print('gravou o frame_%d'% count, frame)
cv2.imshow('Frame', frame)
cap_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
cap_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# Run inference.
start = time.perf_counter()
_, scale = common.set_resized_input(
interpreter, (cap_width, cap_height), lambda size: cv2.resize(im, size)
)
interpreter.invoke()
# Check result.
results = classify.get_classes(interpreter, args.top_k, args.threshold)
elapsed_ms = (time.perf_counter() - start) * 1000
if results:
for i in range(len(results)):
label = "{0} ({1:.2f})".format(labels[results[i][0]], results[i][1])
pos = 60 + (i * 30)
visual.draw_caption(frame, (10, pos), label)
# display
cv2.imshow(WINDOW_NAME, frame)
if cv2.waitKey(10) & 0xFF == ord("q"):
break
This code is used to run inference with coral device. I would like to know how can I do the same thing but without coral? I would like to test the differences between using my model with and without the edge tpu usb device.
Lastly, I have tried Image classification from this link using tensorflow lite. However, I am getting the following error:
RuntimeError: Encountered unresolved custom op: edgetpu-custom-op.Node
number 0 (edgetpu-custom-op) failed to prepare.
I recently came into this for a thesis supervision. We tested face detection in a raspberry pi 4 with Coral USB an without (inference on rpi CPU). Are you using the same model file for both? If this is the case, then this is the problem.
You need to use the bare tflite model for the CPU inference and the TPU-compiled model for the inference with TPU.
You can take a look at this repo where you can find the code I was mentioned before (it's not well documented but it's working, look at the inference CPU and inference CORAL files).
I am new to detectron2 and this is my first project. After reading the docs and using the tutorials as a guide, I trained my model on the custom dataset and performed the evaluation.
I would now like to make predictions on images I receive via an API by loading this saved model. I could not find any reading materials that could help me with this task.
To save my model, I have used this link as a reference - https://detectron2.readthedocs.io/en/latest/tutorials/models.html
I am able to save my trained model using the following code-
from detectron2.modeling import build_model
model = build_model(cfg) # returns a torch.nn.Module
from detectron2.checkpoint import DetectionCheckpointer
checkpointer = DetectionCheckpointer(model, save_dir="output")
checkpointer.save("model_final") # save to output/model_final.pth
But I am still confused as to how I can go about implementing what I want. I could use some guidance on what my next steps should be. Would be extremely grateful to anyone who can help.
for a single image, create a list of data. Put image path in the file_name as below:
test_data = [{'file_name': '.../image_1jpg',
'image_id': 10}]
Then do run the following:
from detectron2.config import get_cfg
from detectron2.engine import DefaultPredictor
from detectron2.data import MetadataCatalog
from detectron2.utils.visualizer import Visualizer, ColorMode
import matplotlib.pyplot as plt
import cv2.cv2 as cv2
test_data = [{'file_name': '.../image_1jpg',
'image_id': 10}]
cfg = get_cfg()
cfg.merge_from_file("model config")
cfg.MODEL.WEIGHTS = "model_final.pth" # path for final model
predictor = DefaultPredictor(cfg)
im = cv2.imread(test_data[0]["file_name"])
outputs = predictor(im)
v = Visualizer(im[:, :, ::-1],
metadata=MetadataCatalog.get(cfg.DATASETS.TRAIN[0]),
scale=0.5,
instance_mode=ColorMode.IMAGE_BW)
out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
img = cv2.cvtColor(out.get_image()[:, :, ::-1], cv2.COLOR_RGBA2RGB)
plt.imshow(img)
This will show the prediction for the single image
I'm trying to load a sequence of PNG images concatenated together as bytes; I know the number of images, but not their individual file sizes.
Loading the first image is easy with something like
import io
from PIL import Image
buffer = io.BytesIO(image_bytearray)
image = Image.open(buffer)
image.load()
However, I'm not sure how to handle the subsequent images. Two approaches that seem to work but might be too brittle:
Split the bytes based on the PNG header e.g. image_bytearray.split(b"\x89PNG\r\n\x1a\n"). This seems to work, but I'm worried in some edge cases this could appear in a non-header location.
Use BytesIO.tell() to determine how much of the stream was read. This appears to be 4 bytes less than the actual file size; I can add 4 to account for this, but I'm not this won't change in a later version.
Here's a simple example that illustrates the two approaches:
import io
import sys
from PIL import Image
import PIL
def setup_bytes():
"""
Concatenate the bytes of some images. The actual images don't matter
"""
files = ["Tests/images/hopper.png", "Tests/images/test-card.png"]
bytes_out = bytes()
for file in files:
im = Image.open(file)
buffer = io.BytesIO()
im.save(buffer, format="PNG")
print(f"writing {len(buffer.getvalue())} bytes")
bytes_out += buffer.getvalue()
return bytes_out
def read_split(bytes_in):
png_header = b"\x89PNG\r\n\x1a\n"
images_out = []
image_byte_splits = bytes_in.split(png_header)
for image_bytes in image_byte_splits:
if len(image_bytes) == 0:
continue
# add back the header
image = Image.open(io.BytesIO(png_header + image_bytes))
image.load()
print(f"read {len(png_header) + len(image_bytes)} bytes")
images_out.append(image)
return images_out
def read_streaming(bytes_in):
images_out = []
bytes_read = 0
# Read the images back from the bytes (without knowing the sizes).
while bytes_read < len(bytes_in):
buffer = io.BytesIO(bytes_in[bytes_read:])
image = Image.open(buffer)
image.load()
images_out.append(image)
# Start the next read at the end of the current image.
# These extra 4 bytes appear necessary.
read = buffer.tell() + 4
print(f"read {read} bytes?")
bytes_read += read
return images_out
def main():
print(f"python sys.version = {sys.version}")
print(f"Pillow version = {PIL.__version__}")
b = setup_bytes()
read_split(b)
read_streaming(b)
main()
My questions are:
Is splitting safe? Is there a chance that the header could also appear in the body of an image?
Is adding an offset to tell() safe? Is there a way to get the image loading to leave the position at the actual end of the file?
Is there a better way to do this in general? Some of the classes in PngImagePlugin.py look like they'd be useful to examine the chunks without actually decompressing.
I wanted to detect and recognize traffic signs from a video feed. I used the Tensorflow ML framework for recognition of signs and used haar classifier for detection of signs.
Here is the code:
import cv2
import numpy as np
import tensorflow as tf
import os,time
import threading
# constants
IMAGE_SIZE = 200.0
MATCH_THRESHOLD = 3
def SignRecognizer():
#to neglect all tensorflow compilation warnings
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
#path to the blob
image_path='/root/Desktop/blob.jpg'
#read the image data
image_data = tf.gfile.FastGFile(image_path,'rb').read()
#load label file,strip off carriage return \n
label_lines= [line.rstrip() for line in tf.gfile.GFile("/root/Desktop/another_model/retrained_labels.txt")]
#unpersists graph from file
with tf.gfile.FastGFile("/root/Desktop/another_model/retrained_graph.pb",'rb') as f:
graph_def=tf.GraphDef()
graph_def.ParseFromString(f.read())
_=tf.import_graph_def(graph_def,name='')
with tf.Session() as sess:
#feed the image_data as input to the graph and get the first prediction
softmax_tensor=sess.graph.get_tensor_by_name("final_result:0")
predictions = sess.run(softmax_tensor,\
{'DecodeJpeg/contents:0':image_data})
#sort to show labels of first prediction in order of confidence
top_k=predictions[0].argsort()[-len(predictions[0]):][::-1]
for node_id in top_k:
human_string=label_lines[node_id]
print("%s"%(human_string))
break
roundabout_cascade = cv2.CascadeClassifier("/root/Desktop/tsp/haarcascade_roundabout.xml")
videocapture = cv2.VideoCapture(0)
scale_factor=1.3
while 1:
ret,pic = videocapture.read()
# do roundabout detection on street image
gray = cv2.cvtColor(pic,cv2.COLOR_RGB2GRAY)
signs = roundabout_cascade.detectMultiScale(pic,scaleFactor=1.4,minNeighbors=6)
# initialize ORB and BFMatcher
orb = cv2.ORB_create()
bf = cv2.BFMatcher(cv2.NORM_HAMMING,crossCheck=True)
# find the keypoints and descriptors for roadsign image
roadsign = cv2.imread("/root/Desktop/tsp/roundabout.jpg",0)
kp_r,des_r = orb.detectAndCompute(roadsign,None)
for (x,y,w,h) in signs:
#cv2.rectangle(pic,(x,y),(x+w,y+h),(255,0,0),2)
# obtain object from street image
obj = gray[y:y+h,x:x+w]
color_image=pic[y:y+h,x:x+w]
cv2.imwrite("/root/Desktop/blob.jpg",color_image)
cv2.imshow('blob', color_image)
#start a new thread and run SignRecognizer on it
t=threading.Thread(name="SignRecognizer",target=SignRecognizer)
#set the thread as a daemon to prevent blocking of the main program
t.setDaemon(True)
t.start()
ratio = IMAGE_SIZE / obj.shape[1]
obj = cv2.resize(obj,(int(IMAGE_SIZE),int(obj.shape[0]*ratio)))
# find the keypoints and descriptors for object
kp_o, des_o = orb.detectAndCompute(obj,None)
if len(kp_o) == 0 or des_o == None:
continue
# match descriptors
matches = bf.match(des_r,des_o)
# draw object on street image, if threshold met
if(len(matches) >= MATCH_THRESHOLD):
cv2.rectangle(pic,(x,y),(x+w,y+h),(255,0,0),2)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(pic,'Roundabout sign',(x,y),font,1,(255,255,255),1,cv2.LINE_AA)
cv2.imshow('roundabout_signs',pic)
k = cv2.waitKey(30) & 0xFF
if k==2:
break
cv2.waitKey(0)
cv2.destroyAllWindows()
The SignRecognizer function reads the blob image file and recognizes the sign using the model I created using tensorflow ML Framework.
I used VideoCapture(0) to start the webcam and simulate a live video feed.
I also used OpenCV's ORB ( Oriented FAST and rotated BRIEF) to remove false positives.
I used threading module to run the SignRecognizer on another thread and set it as a daemon so that the main pgm. wasn't blocked during recognition.
Everything works great but there seems to be a little lag inspite of using threading module.Is there any way to make it lag free?