Annotate for different objects
Opened this issue · 1 comments
Hi mark. I am not able to annotate for different objects. can you please help me? Here's the code.
`import os
import matplotlib.pyplot as plt
import cv2
from matplotlib.widgets import RectangleSelector
from generate_xml import write_xml
global constants
img = None
tl_list = []
br_list = []
object_list = []
constants
image_folder = 'images'
savedir = 'annotations'
obj = 'Blight'
obj2 = 'Downy Mildew'
obj3 = 'Leaf Spot'
obj4 = 'Webworm'
obj5 = 'Aphids'
obj6 = 'Leafminers'
obj7 = 'Healthy'
def line_select_callback(clk, rls):
global tl_list
global br_list
global object_list
tl_list.append((int(clk.xdata), int(clk.ydata)))
br_list.append((int(rls.xdata), int(rls.ydata)))
object_list.append(obj)
object_list.append(obj2)
object_list.append(obj3)
object_list.append(obj4)
object_list.append(obj5)
object_list.append(obj6)
object_list.append(obj7)
def onkeypress(event):
global object_list
global tl_list
global br_list
global img
if event.key == 'q':
print (object_list.append(obj2))
write_xml(image_folder, img, object_list, tl_list, br_list, savedir)
tl_list = []
br_list = []
object_list = []
img = None
elif event.key == 'd':
print(object_list.append(obj3))
write_xml(image_folder, img, object_list, tl_list, br_list, savedir)
tl_list = []
br_list = []
object_list = []
img = None
def toggle_selector(event):
toggle_selector.RS.set_active(True)
if name == 'main':
for n, image_file in enumerate(os.scandir(image_folder)):
img = image_file
fig, ax = plt.subplots(1, figsize=(10.5, 8))
mngr = plt.get_current_fig_manager()
mngr.window.setGeometry(250, 40, 800, 600)
image = cv2.imread(image_file.path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
ax.imshow(image)
toggle_selector.RS = RectangleSelector(
ax, line_select_callback,
drawtype='box', useblit=True,
button=[1], minspanx=5, minspany=5,
spancoords='pixels', interactive=True,
)
bbox = plt.connect('key_press_event', toggle_selector)
key = plt.connect('key_press_event', onkeypress)
plt.tight_layout()
plt.show()
plt.close(fig)
`
whenever i run the code. i get an error like this
None Traceback (most recent call last): File "C:\Users\User\Anaconda3\lib\site-packages\matplotlib\cbook\__init__.py", line 388, in process proxy(*args, **kwargs) File "C:\Users\User\Anaconda3\lib\site-packages\matplotlib\cbook\__init__.py", line 228, in __call__ return mtd(*args, **kwargs) File "part6.py", line 53, in onkeypress write_xml(image_folder, img, object_list, tl_list, br_list, savedir) File "C:\Users\User\Desktop\Disease-detection\generate_xml.py", line 11, in write_xml image = cv2.imread(img.path) AttributeError: 'NoneType' object has no attribute 'path'
first element from scandir in 'for n, image_file in enumerate(os.scandir(image_folder)):' is not an image file but, './ds_store'. so use second element onwards..