The ERROR
UnboundLocalError: cannot access local variable 'res' where it is not associated with a value
from sketchpy import canvas
obj = canvas.sketch_from_svg("23.svg")
obj.draw()
print(obj)
Related
I define my dictionary 'frame_dict' outside my for loop. However, when it gets to my forFrame function, despite setting it has a global variable, I get an error saying that frame_dict is not defined. Any help?
import os
from imageai.Detection import VideoObjectDetection
import pickle
PATH_TO_STORE_VIDEOS = "/Users/jaime.pereira/Library/CloudStorage/OneDrive-OneWorkplace/Benchmark_Project/Videos"
tv_commercial_videos = os.listdir('Videos/')
def yolo_neural_network(path_to_videos, tv_commercials):
execution_path = os.getcwd()
frame_dict = {}
for tv_c in tv_commercials:
frame_dict.setdefault(tv_c,[])
# Use pre trained neural network to label things in videos
vid_obj_detect = VideoObjectDetection()
# Set and load Yolo model
vid_obj_detect.setModelTypeAsYOLOv3()
vid_obj_detect.setModelPath(os.path.join(execution_path,"yolov3.pt"))
vid_obj_detect.loadModel()
input_file_path = os.path.join(path_to_videos, tv_c)
if not os.path.exists("output_from_model_yolov3/"):
os.makedirs("output_from_model_yolov3/")
output_file_path = os.path.join(execution_path,"output_from_model_yolov3/", "model_yolov3_output_" + tv_c)
def forFrame(frame_number, output_array, output_count):
global frame_dict
frame_dict[tv_c].append(output_count)
return frame_dict
vid_obj_detect.detectObjectsFromVideo(
input_file_path=input_file_path,
output_file_path=output_file_path,
log_progress=True,
frame_detection_interval= 60,
minimum_percentage_probability=70,
per_frame_function=forFrame,
save_detected_video=True
)
# save dictionary
f = open("yolo_dict.pkl", "wb")
# write dict to pickle file
pickle.dump(frame_dict, f)
# close file
f.close()
return frame_dict
yolo = yolo_neural_network(PATH_TO_STORE_VIDEOS, tv_commercial_videos)
Exception has occurred: ValueError
An error occured. It may be that your input video is invalid. Ensure you specified a proper string value for 'output_file_path' is 'save_detected_video' is not False. Also ensure your per_frame, per_second, per_minute or video_complete_analysis function is properly configured to receive the right parameters.
File "/Users/jaime.pereira/Library/CloudStorage/OneDrive-OneWorkplace/Benchmark_Project/debug.py", line 35, in forFrame
frame_dict[tv_c].append(output_count)
NameError: name 'frame_dict' is not defined
During handling of the above exception, another exception occurred:
File "/Users/jaime.pereira/Library/CloudStorage/OneDrive-OneWorkplace/Benchmark_Project/debug.py", line 38, in yolo_neural_network
vid_obj_detect.detectObjectsFromVideo(
File "/Users/jaime.pereira/Library/CloudStorage/OneDrive-OneWorkplace/Benchmark_Project/debug.py", line 59, in <module>
I tried setting my frame_dict variable as global inside the forframe function expecting it to recognise it.
frame_dict is not a global, it is just in an outer scope, remove global keyword
Since you mutate the object, you don't need to do anything more:
def forFrame(frame_number, output_array, output_count):
frame_dict[tv_c].append(output_count)
return frame_dict
Since you don't assign anything to frame_dict, even if the variable were a global variable, you wouldn't need to add the global keyword if you mutate the object. global is useful only if you need to assign a new value to the variable.
The problem you are facing is that frame_dict is actually not a global variable. It is defined inside of yolo_neural_network. While this is indeed outside forFrame, it is not a global variable.
In this scenario, you should simply remove the global statement, because it is not a global variable you are importing.
I have this code to run test on Jenkins cloud, the run works fine but on my machine a get this error : UnboundLocalError: local variable
My code :
# -*- coding: utf-8 -*-
import time
import unittest
import os
from selenium import webdriver
from com.POMProject.Pages.homePage import HomePage
from com.POMProject.Pages.loginPage import LoginPage
from com.POMProject.Setup.urls import beta_url
from com.POMProject.Setup.users import *
class LoginTest(unittest.TestCase):
#classmethod
def setUp(cls):
if os.getenv('CHROMEWEBDRIVER'):
chromewebdriverbin = os.getenv('CHROMEWEBDRIVER')
cls.driver = webdriver.Chrome(
executable_path=chromewebdriverbin)
cls.driver.implicitly_wait(50)
cls.driver.delete_all_cookies()
cls.driver.set_window_size(1224, 800)
def test_login_valid_user_password(self):
driver = self.driver
driver.get(beta_url)
login = LoginPage(driver)
home = HomePage(driver)
home.click_user_icon()
home.click_login_button()
login.enter_user(user_email)
login.click_next_button()
login.enter_password(user_password)
login.click_submit_button()
driver.quit()
print('Test Completed')
The error message:
E UnboundLocalError: local variable 'chromewebdriverbin' referenced before assignment
This error message...
UnboundLocalError: local variable 'chromewebdriverbin' referenced before assignment
...implies that the varible was referenced even before it was assigned a value.
Analysis
The main issue is, the following condition fails:
if os.getenv('CHROMEWEBDRIVER'):
Hence, the following line:
chromewebdriverbin = os.getenv('CHROMEWEBDRIVER')
never gets executed and the variable chromewebdriverbin is never assigned any value.
In this circumstance, when you refer to chromewebdriverbin in the line:
cls.driver = webdriver.Chrome(executable_path=chromewebdriverbin)
The above mentioned error is raised.
Reason
The most probable reason is, the environment variable CHROMEWEBDRIVER isn't set on the failing machine.
Fixed by adding chromedriver in local machine :
def setUp(cls):
if os.getenv('CHROMEWEBDRIVER'):
chromewebdriverbin = os.getenv('CHROMEWEBDRIVER')
else:
chromewebdriverbin = '/usr/bin/chromedriver'
cls.driver = webdriver.Chrome(
executable_path=chromewebdriverbin)
I am starting with python. I am trying a very simple class-structure, but i get an error.
This is my script:
class controller:
def initLocal(self):
path = input('path:')
local = local()
local.path = path
return local
class location:
pass
class local(location):
path = None
controller = controller()
local = controller.initLocal()
And this is the result i get in the console:
path:a
Traceback (most recent call last):
File "path\to\test.py", line 21, in <module>
local = controller.initLocal();
File "path\to\test.py", line 5, in initLocal
local = local();
UnboundLocalError: local variable 'local' referenced before assignment
I searched for this error, and found it usually has to do something with uncorrect scopes. I however do not see what i am doing wrong here. Is it 'illegal' to have a class instance with the same name as the class?
If i change the initLocal() method to this:
def initLocal(self):
path = input('path:')
locale = local()
locale.path = path
return locale
It works, but i cannot find out why, since controller = controller() does not cause any problems.
Can somebody tell me what i am doing wrong? I have the feeling it might be something really obvious, but i cannot figure out what it is.
class Location:
pass
class Local(location):
path = None
class Controller:
def initLocal(self):
path = raw_input('path:')
local = Local()
local.path = path
return local
controller = Controller()
local = controller.initLocal()
I have made a tkinter program where i keep getting this error:
File "F:\Programming 2\Gui\Gui #11.py", line 78, in shape_workit
cylinder(pos=(0,0,0),axis=(1,0,0),color=self.color3.get(),
AttributeError: 'Kinter' object has no attribute 'color3'
Here is the code which the error occurs from:
def shapescolor(self):
if self.color1.get()=="Does a Orange":
color3=color.orange
if self.color1.get()=="Does a Blue":
color3=color.blue
def shape_workit(self):
try:
if self.shape.get()=="Does a Cylinder": #Creates Cylinder
cylinder(pos=(0,0,0),axis=(1,0,0),color=self.color3.get() ##ERROR HERE,
radius=float(self.radius.get()))
Here is the Code where the error it gets from
My guess is that you need to be doing self.color3 = ... rather than color3 = ..., since you're later refering to self.color3 and haven't set that attribute anywhere else in the code you posted.
I have two files, one is in the webroot, and another is a bootstrap located one folder above the web root (this is CGI programming by the way).
The index file in the web root imports the bootstrap and assigns a variable to it, then calls a a function to initialize the application. Everything up to here works as expected.
Now, in the bootstrap file I can print the variable, but when I try to assign a value to the variable an error is thrown. If you take away the assignment statement no errors are thrown.
I'm really curious about how the scoping works in this situation. I can print the variable, but I can't asign to it. This is on Python 3.
index.py
# Import modules
import sys
import cgitb;
# Enable error reporting
cgitb.enable()
#cgitb.enable(display=0, logdir="/tmp")
# Add the application root to the include path
sys.path.append('path')
# Include the bootstrap
import bootstrap
bootstrap.VAR = 'testVar'
bootstrap.initialize()
bootstrap.py
def initialize():
print('Content-type: text/html\n\n')
print(VAR)
VAR = 'h'
print(VAR)
Thanks.
Edit: The error message
UnboundLocalError: local variable 'VAR' referenced before assignment
args = ("local variable 'VAR' referenced before assignment",)
with_traceback = <built-in method with_traceback of UnboundLocalError object at 0x00C6ACC0>
try this:
def initialize():
global VAR
print('Content-type: text/html\n\n')
print(VAR)
VAR = 'h'
print(VAR)
Without 'global VAR' python want to use local variable VAR and give you "UnboundLocalError: local variable 'VAR' referenced before assignment"
Don't declare it global, pass it instead and return it if you need to have a new value, like this:
def initialize(a):
print('Content-type: text/html\n\n')
print a
return 'h'
----
import bootstrap
b = bootstrap.initialize('testVar')