I got an error when I use the code in cmd by using the code:
python generate_tfrecord.py --csv_input=images\train_labels.csv --image_dir=images\train --output_path=train.record""
Usage:
# From tensorflow/models/
# Create train data:
python generate_tfrecord.py --csv_input=data/train_labels.csv --output_path=train.record
# Create test data:
python generate_tfrecord.py --csv_input=data/test_labels.csv --output_path=test.record
"""
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
import os
import io
import pandas as pd
import tensorflow as tf
from PIL import Image
from object_detection.utils import dataset_util
from collections import namedtuple, OrderedDict
flags = tf.compat.v1.flags
flags.DEFINE_string('csv_input', '', 'Path to the CSV input')
flags.DEFINE_string('output_path', '', 'Path to output TFRecord')
flags.DEFINE_string('image_dir', '', 'Path to images')
FLAGS = flags.FLAGS
# TO-DO replace this with label map
def class_text_to_int(row_label):
if row_label == 'put your selected items':
return 1
else:
None
def split(df, group):
data = namedtuple('data', ['filename', 'object'])
gb = df.groupby(group)
return [data(filename, gb.get_group(x)) for filename, x in zip(gb.groups.keys(), gb.groups)]
def create_tf_example(group, path):
with tf.gfile.GFile(os.path.join(path, '{}'.format(group.filename)), 'rb') as fid:
encoded_jpg = fid.read()
encoded_jpg_io = io.BytesIO(encoded_jpg)
image = Image.open(encoded_jpg_io)
width, height = image.size
filename = group.filename.encode('utf8')
image_format = b'jpg'
xmins = []
xmaxs = []
ymins = []
ymaxs = []
classes_text = []
classes = []
for index, row in group.object.iterrows():
xmins.append(row['xmin'] / width)
xmaxs.append(row['xmax'] / width)
ymins.append(row['ymin'] / height)
ymaxs.append(row['ymax'] / height)
classes_text.append(row['class'].encode('utf8'))
classes.append(class_text_to_int(row['class']))
tf_example = tf.train.Example(features=tf.train.Features(feature={
'image/height': dataset_util.int64_feature(height),
'image/width': dataset_util.int64_feature(width),
'image/filename': dataset_util.bytes_feature(filename),'image/source_id': dataset_util.bytes_feature(filename),
'image/encoded': dataset_util.bytes_feature(encoded_jpg),
'image/format': dataset_util.bytes_feature(image_format),
'image/object/bbox/xmin': dataset_util.float_list_feature(xmins)'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
'image/object/class/label': dataset_util.int64_list_feature(classes),
}))
return tf_example
def main(_):
writer = tf.python_io.TFRecordWriter(FLAGS.output_path)
path = os.path.join(FLAGS.image_dir)
examples = pd.read_csv(FLAGS.csv_input)
grouped = split(examples, 'filename')
for group in grouped:
tf_example = create_tf_example(group, path)
writer.write(tf_example.SerializeToString())
writer.close()
output_path = os.path.join(os.getcwd(), FLAGS.output_path)
print('Successfully created the TFRecords: {}'.format(output_path))
if __name__ == '__main__':
tf.app.run()
tf.app.run()
The error message got was:
Traceback (most recent call last): File "generate_tfrecord.py", line
102, in
tf.app.run()
AttributeError: module 'tensorflow' has no attribute 'app'
Can any one help me?
If you're using TensorFlow v2, app.run has been moved to tf.compat.v1.app.run, as shown here.
If you downgrade your tensorflow to other version i.e 1.x.
It should work fine then.
Do that by using the command pip install tensorflow==1.7.0
just try replacing import tensorflow as tf with import tensorflow.compat.v1 as tf and nothing else, i was facing the same issue .This worked for me.
use abseil if you don't want to downgrade
from absl import app
if __name__ == '__main__':
app.run(main)
Related
i was trying to make the user input file audio in wav or mp3 format to low pass filter it
i am supposed to use tkinter to create GUI so that use can use two buttons one to input file and other to call function runlow which is the filter i dont know where i went wrong
i don't know why i need to add details to the question though stackflow ?
but i got this error :
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\ezz\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "C:\Users\ezz\desktop\voicerecorder.py", line 173, in runlow
filtered = running_mean(channels[0], N).astype(channels.dtype)
TypeError: 'NoneType' object is not subscriptable
here is the main code
from cProfile import label
from doctest import master
from importlib.metadata import files
from pydoc import text
from tkinter import *
from tkinter import filedialog
import matplotlib.pyplot as plt
from functools import partial
import numpy as np
from tkinter.filedialog import asksaveasfile
from tkinter import ttk
import wave
import math
import contextlib
import warnings
outname = (r"filtered.wav")
def fileaudio():
global fname1
fname1 = filedialog.askopenfilename(filetypes=(("Audio files", "*.wav;*.mp3"),
("All files", "*.*") ))
print(fname1)
return fname1
warnings.simplefilter("ignore", DeprecationWarning)
# Change label contents
root =Tk()
root.geometry("1280x800")
root.resizable(False,False)
root.title("Audio filter")
root.configure(background="white")
#icon
image_icon=PhotoImage(file="filter.png")
root.iconphoto(False,image_icon)
#logo
photo=PhotoImage(file="filter.png")
myimage=Label(image=photo,background="white")
myimage.pack(padx=5,pady=5)
#name
Label(text="Audio filter",font="ariel 30 bold",background="black",fg="white").pack()
#entry box
def runlow():
cutOffFrequency = 400.0
# from http://stackoverflow.com/questions/13728392/moving-average-or-running-mean
def running_mean(x, windowSize):
cumsum = np.cumsum(np.insert(x, 0, 0))
return (cumsum[windowSize:] - cumsum[:-windowSize]) / windowSize
# from http://stackoverflow.com/questions/2226853/interpreting-wav-data/2227174#2227174
def interpret_wav(raw_bytes, n_frames, n_channels, sample_width, interleaved = True):
if sample_width == 1:
dtype = np.uint8 # unsigned char
elif sample_width == 2:
dtype = np.int16 # signed 2-byte short
else:
raise ValueError("Only supports 8 and 16 bit audio formats.")
channels = np.fromstring(raw_bytes, dtype=dtype)
if interleaved:
# channels are interleaved, i.e. sample N of channel M follows sample N of channel M-1 in raw data
channels.shape = (n_frames, n_channels)
channels = channels.T
else:
# channels are not interleaved. All samples from channel M occur before all samples from channel M-1
channels.shape = (n_channels, n_frames)
return channels
with contextlib.closing(wave.open(fname1,'rb')) as spf:
sampleRate = spf.getframerate()
ampWidth = spf.getsampwidth()
nChannels = spf.getnchannels()
nFrames = spf.getnframes()
# Extract Raw Audio from multi-channel Wav File
signal = spf.readframes(nFrames*nChannels)
spf.close()
channels = interpret_wav(signal, nFrames, nChannels, ampWidth, True)
# get window size
# from http://dsp.stackexchange.com/questions/9966/what-is-the-cut-off-frequency-of-a-moving-average-filter
freqRatio = (cutOffFrequency/sampleRate)
N = int(math.sqrt(0.196196 + freqRatio**2)/freqRatio)
# Use moviung average (only on first channel)
filtered = running_mean(channels[0], N).astype(channels.dtype)
wav_file = wave.open(outname, "w")
wav_file.setparams((1, ampWidth, sampleRate, nFrames, spf.getcomptype(), spf.getcompname()))
wav_file.writeframes(filtered.tobytes('C'))
wav_file.close()
#button
record=Button(root,font="ariel 20",text="Input",bg="black",fg="white",border=0,command=fileaudio).pack(pady=30)
record2=Button(root,font="ariel 20",text="Filter",bg="black",fg="white",border=0,command=runlow).pack(pady=30)
#functions to integrate
# from http://stackoverflow.com/questions/13728392/moving-average-or-running-mean
root.mainloop()
You forgot to return the filename from your input() function.
def input():
fname = filedialog.askopenfilename(filetypes=(("Audio files", "*.wav;*.mp3"),
("All files", "*.*") ))
print(fname)
return fname # <== you dropped this
fname = input()
Also there's another error, you must use askinputfilename, not askinputfile.
FWIW you should not use the name input() for your function since it overwrites and disables python's built-in input function.
I’m trying to solve the below question on Hackerrank.
It is working on Pycharm IDE but on Hackerrank the output is not matching with the expected output given. Below is the Code I used
import os
import builtins
import pickle
import sys
sys.tracebacklimit = 0
import traceback
import io
from logging import Logger
safe_builtins = {
'range',
'complex',
'set',
'frozenset'
}
class RestrictedUnpickler(pickle.Unpickler):
def find_class(self, module, name):
# Only allow safe classes from builtins.
if module == "builtins" and name in safe_builtins:
return getattr(builtins, name)
# Forbid everything else.
raise pickle.UnpicklingError("global '%s.%s' is forbidden" %
(module, name))
def restricted_loads(s):
"""Helper function analogous to pickle.loads()."""
return RestrictedUnpickler(io.BytesIO(s)).load()
def func1(a):
try:
x = restricted_loads(pickle.dumps(a))
return a
except pickle.UnpicklingError:
s = traceback.format_exc()
return s
def func2(s):
try:
x = restricted_loads(pickle.dumps(slice(0, 8, 3)))
return s[x]
except pickle.UnpicklingError:
s = traceback.format_exc()
return s
if __name__ == "__main__":
a = range(int(input()))
b = func1(a)
print(b)
y = tuple(input())
z = func2(y)
print(z)
Expected Output:
range(0, 50):
Traceback (most recent call last):
_pickle.UnpicklingError: global 'builtins.slice' is forbidden
Actual Output:
range(0, 50):
_pickle.UnpicklingError: global 'builtins.slice' is forbidden
Question: Why does the output not match in hackerrank when the code seems to be correct?
This is an acceptable answer for Hackerrank. I included sys.stdout.write("Traceback (most recent call last):\n") since we declared the traceback=0 initially and pickle.UnpicklingError class doesn't have a traceback message. Try the below code for Hacker rank.
def func2(s):
try:
x = restricted_loads(pickle.dumps(slice(0, 8, 3)))
return s[x]
except pickle.UnpicklingError as e :
sys.stdout.write("Traceback (most recent call last):\n")
s = traceback.format_exc()
return s
PS: This is just an acceptable answer for Hackerrank. I just made simple modifications according to the output needs. This might not be a good practice everywhere. And I dont have full knowledge of it either.
import os
import builtins
import pickle
import sys
sys.tracebacklimit=0
import traceback
import io
from logging import Logger
safe_builtins = {
'range',
'complex',
'set',
'frozenset'
}
class RestrictedUnpickler(pickle.Unpickler):
def find_class(self, module, name):
# Only allow safe classes from builtins.
if module == "builtins" and name in safe_builtins:
return getattr(builtins, name)
# Forbid everything else.
raise pickle.UnpicklingError("global '%s.%s' is forbidden" %
(module, name))
def restricted_loads(s):
"""Helper function analogous to pickle.loads()."""
return RestrictedUnpickler(io.BytesIO(s)).load()
def func1(a):
try:
x = restricted_loads(pickle.dumps(a))
return a
except pickle.UnpicklingError:
s = traceback.format_exc()
return s
def func2(s):
try:
x = restricted_loads(pickle.dumps(slice(0, 8, 3)))
return s[x]
except pickle.UnpicklingError as e :
sys.stdout.write("Traceback (most recent call last):\n")
s = traceback.format_exc()
return s
if __name__ == "__main__":
a = range(int(input()))
b = func1(a)
print(b)
y = tuple(input())
z = func2(y)
print(z)
I have a publisher in ROS 2 which publishes an image message as following:
#!/usr/bin/env python3
# Revision $Id$
import rclpy
from rclpy.node import Node
from std_msgs.msg import String
from cv_bridge import CvBridge
from sensor_msgs.msg import Image
import cv2
import numpy as np
class MinimalPublisher(Node):
def __init__(self):
super().__init__('minimal_publisher')
self.publisher_ = self.create_publisher(Image, 'Image', 10)
timer_period = 0.5 # seconds
self.timer = self.create_timer(timer_period, self.timer_callback)
self.i = 0
self.im_list = []
self.cv_image = cv2.imread('test.jpg') ### an RGB image
self.bridge = CvBridge()
def timer_callback(self):
self.publisher_.publish(self.bridge.cv2_to_imgmsg(np.array(self.cv_image), "bgr8"))
self.get_logger().info('Publishing an image')
def main(args=None):
rclpy.init(args=args)
minimal_publisher = MinimalPublisher()
rclpy.spin(minimal_publisher)
minimal_publisher.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()
It works fine with a single image. But I want to publish a batch of images with the given shape:
[12, 3, 224, 224] => [batch, channel, width, height]
I tried to send it as a list of images but it failed. How to do so?
More info:
python 3.6
ROS 2 - eloquent (build from source)
Ubuntu 18
Finally, I built a custom message as follows:
sensor_msgs/Image[ 2 ] data
The batch size is 2.
Then, add it to my publisher as follows:
#!/usr/bin/env python3
# Revision $Id$
import rclpy
from rclpy.node import Node
from custom_batch.msg import Batch #### import custom message
from cv_bridge import CvBridge
from sensor_msgs.msg import Image
import cv2
import numpy as np
class MinimalPublisher(Node):
def __init__(self):
super().__init__('minimal_publisher')
self.publisher_ = self.create_publisher(Batch, 'Image', 10) #### change here
timer_period = 0.5 # seconds
self.timer = self.create_timer(timer_period, self.timer_callback)
self.i = 0
self.im_list = []
self.cv_image1 = cv2.imread('3.jpg')
self.cv_image2 = cv2.imread('2.jpg')
self.bridge = CvBridge()
def timer_callback(self):
#### custom message
my_msg = Batch()
my_msg.data[0] = self.bridge.cv2_to_imgmsg(np.array(self.cv_image1), "bgr8")
my_msg.data[1] = self.bridge.cv2_to_imgmsg(np.array(self.cv_image2), "bgr8")
#####
self.publisher_.publish(my_msg) ## custom message
self.get_logger().info('Publishing a batch of images')
def main(args=None):
rclpy.init(args=args)
minimal_publisher = MinimalPublisher()
rclpy.spin(minimal_publisher)
minimal_publisher.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()
You can find useful instruction about custom message in ROS2 here.
I want two scripts to communicate in python. I want them to know if the others have failed.
Doing what you see in the picture I have some questions. When I run rob2.py I automatically run rob1.py, why is this?
rob1.py
import simpy
import time
from rob2 import brok2
class Moving:
def __init__(self, env):
self.env = env
"""What does self.prov do?"""
self.prov = env.process(self.work())
self.broken = False
if self.broken == False:
global brok1
brok1 = 0
else:
brok1 = 0
#print(brok1)
def work(self):
while True:
yield env.timeout(20)
if brok2 == 1:
print("Robot 2 is not broken")
else:
print("Robot 2 is broken")
env= simpy.Environment()
moving = Moving(env)
env.run(until = 60)
rob2.py
import simpy
import time
from rob1 import brok1
class Placing:
def __init__(self, env):
self.env = env
"""What does self.prov do?"""
self.prov = env.process(self.work())
self.broken = False
if self.broken == False:
global brok2
brok2=1
else:
brok2 = 0
def work(self):
while True:
yield env.timeout(20)
time.sleep(5)
if brok1 == 1:
print("Robot 1 is not broken")
else:
print("Robot 1 is broken")
env= simpy.Environment()
placing = Placing(env)
env.run(until = 60)
And what have I done wrong when I get this message trying to run the scripts?
Traceback (most recent call last):
File "rob2.py", line 3, in <module>
from rob1 import brok1
File "/Users/erik/Python/python/rob1.py", line 3, in <module>
from rob2 import brok2
File "/Users/erik/Python/python/rob2.py", line 3, in <module>
from rob1 import brok1
ImportError: cannot import name 'brok1'
I came across some posts about zeroMQ, is that the way to go here?
The ImportError exception is thrown because Python detects a circular import loop: in one of your files you import the other one, which itself imports the first one, which import the second one and so on.
You will need to reorganize your code to avoid that.
Most (all?) programming languages will not like a circular import one way or another.
import numpy as np
import os
import random
from six.moves import cPickle as pickle
from tensorflow.python.platform import gfile
import glob
import TensorflowUtils as utils
DATA_URL = 'http:\\data.csail.mit.edu\\places\\ADEchallenge\\ADEChallengeData2016.zip'
#download and read dataset
def read_dataset(data_dir):
pickle_filename = "MITSceneParsing.pickle"
pickle_filepath = os.path.join(data_dir, pickle_filename)
if not os.path.exists(pickle_filepath):
utils.maybe_download_and_extract(data_dir, DATA_URL, is_zipfile=True)
SceneParsing_folder = os.path.splitext(DATA_URL.split("/")[-1])[0]
result = create_image_lists(os.path.join(data_dir, SceneParsing_folder))
print ("Pickling ...")
with open(pickle_filepath, 'wb') as f:
pickle.dump(result, f, pickle.HIGHEST_PROTOCOL)
else:
print ("Found pickle file!")
with open(pickle_filepath, 'rb') as f:
result = pickle.load(f)
training_records = result['training']
validation_records = result['validation']
del result
return training_records, validation_records
train_records, valid_records = read_dataset('Data_zoo/MIT_SceneParsing')
print(len(train_records))
print(len(valid_records))
the result is:Found pickle file! 0 0
why the lens about train_records and valid_records are 0?
i don't know whree is wrong and how to correct it.
This code is right. The bug is in 'create_image_lists'.
Note this code in create_image_lists:
filename = os.path.splitext(f.split('/')[-1])[0]
This is no problem in Linux, but in windows, the separator is '\\', so you should modify this code to:
filename = os.path.splitext(f.split('\\')[-1])[0]
Then delete this file 'MITSceneParsing.pickle', and run read_dataset again.