HOW TO SET WINDOW BACKGROND IN URSINA ENGINE (PYTHON) - python

I can't set my game's window backgroun image.
This is what I have tried:
mainbg = Sky(texture = 'Assets/ruined_city_main_bg')
But that was incomprehensible and scary.
Also I have tried:
mainbg = Entity(parent = camera.ui, '''All other arguments''', texture = 'Assets/ruined_city_main_bg', position = (0, 0))
The "ENTITY BG" is not showing.

You can't see it because you didn't give it a model. Try adding model='quad'.
mainbg = Entity(parent=camera.ui, model='quad', texture='ruined_city_main_bg')

it looks like you are not setting the file extension.
For example:
ruined_city_main_bg.png
where .png is an image extension.
or try:
skybox_image = load_texture("sky_sunset.jpg")
Sky(texture=skybox_image)
Don't forget the file extension.

Related

ipywidgets: button.on_click() has output delay

Introduction
I am trying to make a small tool for classifying images using the ipywidgets in a Jupyter Notebook, but I am having some trouble aligning the classes and the images. Do you have any suggestion how to fix this.
What I did
import ipywidgets as widgets
from IPython.display import display
import glob
# My images
image_paths = glob.glob("./images/*.png")
# Display image
def display_image(path):
file = open(path, "rb")
image = file.read()
return widgets.Image(
value=image,
format='png',
width=700,
height=700,
)
# Dropdown
def create_dropdown():
return widgets.Dropdown(
options=["1","2","3","4","5","6","7","8","9","10"],
value='5',
description='Category:',
disabled=False
)
# Creating widgets
input_dropdown = create_dropdown()
button = widgets.Button(description="Submit")
output_image = widgets.Image()
output_image.value = display_image(image_paths[-1]).value
# Define function to bind value of the input to the output variable
def bind_input_to_output(sender):
image_path = image_paths[-1]
image_score = input_dropdown.value
next_image_path = image_paths.pop()
print(image_score, image_path)
output_image.value = display_image(next_image_path).value
# Tell the text input widget to call bind_input_to_output() on submit
button.on_click(bind_input_to_output)
# Displaying widgets
display(output_image, input_dropdown, button)
Results
With the above code I end up categorising the upcoming picture, but I really don't understand why. It seems the widgets does not update the image the first time I press the button.
def bind_input_to_output(sender):
image_path = image_paths.pop()
image_score = input_dropdown.value
next_image_path = image_paths[-1]
print(image_score, image_path)
output_image.value = display_image(next_image_path).value
pop first and give next filename at last item

Opencv GUI python: arrange the created buttons

I use python 3.8 and Opencv in Linux.
I have several buttons that have stacked horizontally. How can I arrange them as I like (e.g., in a grid way?)
Is it possible to show some icons for each of the buttons?
Is it possible to make the fonts of the buttons bar larger?
Part of my script: (any suggestion to make my script better is appreciated)
if __name__== "__main__":
Folder_name = "male"
data_path = "path/to/images"
data_path = os.path.join(data_path, Folder_name)
all_imgs_path = glob.glob("{}/*.jpg".format(data_path))
all_imgs_path = sorted(all_imgs_path)
annot = annotation_tool(nof_imgs=len(all_imgs_path))
for index, im_dir in enumerate(all_imgs_path):
annot[index] = im_dir
item_path = "guid.jpg"
img = cv2.imread(item_path)
img_name = item_path.split("/")[-1]
cv2.imshow("{}".format(img_name), img)
cv2.createButton('Next', annot.Next, ["Next Image"])
cv2.createButton('Back', annot.Back, ["Previous Image"])
cv2.createButton('Submit', annot.Submit, ["Submit"])
# there are many of these buttons
UB_Tshirt = cv2.createButton("UB_Tshirt", annot.checkbox, "UB_Tshirt", 1, 0)
UB_Shirt = cv2.createButton("UB_Shirt", annot.checkbox, "UB_Shirt", 1, 0)
UB_Coat = cv2.createButton("UB_Coat", annot.checkbox, "UB_Coat", 1, 0)
cv2.waitKey(0)
print("end")
Edit:
As you see in the image, the buttons bar is very long and goes out of the screen. I would like to create a button pad that is squared.
This isn't a complete answer, but in regards to button arrangement, you have a little control using 'cv2.QT_NEW_BUTTONBAR'.
There's further detail here: https://docs.opencv.org/4.x/dc/d46/group__highgui__qt.html#gad15c7adb377e778dc907c0e318be193e

Pythonic way to load image resources

I have a file called resources.py which loads images to be used in the main project.
So far the code looks like this:
import pyglet
pyglet.resource.path = ["../resources", "C:/users/____/Pictures/useful icons"]
pyglet.resource.reindex()
checkbox_unchecked = pyglet.resource.image("checkbox_unchecked.png")
checkbox_checked = pyglet.resource.image("checkbox_checked.png")
checkbox_unchecked_dark = pyglet.resource.image("checkbox_unchecked_dark.png")
checkbox_checked_dark = pyglet.resource.image("checkbox_checked_dark.png")
checkbox_unchecked_thick = pyglet.resource.image("checkbox_unchecked_thick.png")
checkbox_checked_thick = pyglet.resource.image("checkbox_checked_thick.png")
checkbox_unchecked_disabled = pyglet.resource.image("checkbox_unchecked_disabled.png")
checkbox_checked_disabled = pyglet.resource.image("checkbox_checked_disabled.png")
I thought that this is an unwieldy way to do it, so what came to my mind is something like:
import pyglet
pyglet.resource.path = ['../resources', "C:/users/____/Pictures/useful icons"]
pyglet.resource.reindex()
images = ["checkbox_unchecked.png", "checkbox_checked.png", ...]
for image in images:
exec(f'{image} = pyglet.resource.image("{image}")')
This of course uses the exec function which I know is frowned upon as there is usually a better way of doing it. The only other way I can see of doing it is creating a dictionary instead.
Like so:
import pyglet
pyglet.resource.path = ['../resources', "C:/users/____/Pictures/useful icons"]
pyglet.resource.reindex()
images = ["checkbox_unchecked.png", "checkbox_checked.png", ...]
imageDict = {}
for image in images:
imageDict[image] = pyglet.resource.image(image)
Which of these (or other methods) is the most DRY-complient and
comprehensible way to load the images?
You might consider a dictionary comprehension in combination with the pathlib module so that when you call keys from the dictionary you don't have to call them with the extension
from pathlib import Path
import pyglet
pyglet.resource.path = ['../resources', "C:/users/____/Pictures/useful icons"]
pyglet.resource.reindex()
images = ["checkbox_unchecked.png", "checkbox_checked.png", ...]
imageDict = { Path(image).stem: pyglet.resource.image(image) for image in images }
Then you would get your images out with:
imageDict['checkbox_unchecked']
You can use your dictionary solution to get what you originally wanted by using globals(), which is a dict of all the global variables.
for image in images:
globals()[image.split('.')[0]] = pyglet.resource.image(image)
Or:
globals().update((image.split('.')[0], pyglet.resource.image(image)) for image in images)

NAO fails to save captured image to local computer

I'm trying to save a captured 640x480 RGB image with NAO's front camera to my computer. I'm using python and PIL to do so. Unfortunately, the image just won't save on my computer, no matter what image type or path I use for the parameters of the Image.save()- Method. the image created with PIL contains valid RGB-information though. Here's my code sample from choregraphe:
import Image
def onInput_onStart(self):
cam_input = ALProxy("ALVideoDevice")
nameId = cam_input.subscribeCamera("Test_Cam", 1, 2, 13, 20)
image = cam_input.getImageRemote(nameId) #captures an image
w = image[0] #get the image width
h = image[1] #get the image height
pixel_array = image[6] #contains the image data
result = Image.fromstring("RGB", (w, h), pixel_array)
#the following line doesnt work
result.save("C:\Users\Claudia\Desktop\NAO\Bilder\test.png", "PNG")
cam_input.releaseImage(nameId)
cam_input.unsubscribe(nameId)
pass
Thank you so much for your help in advance!
- a frustrated student
In the comment, you say the code is pasted from choregraphe, so I guess you launch it using choregraphe.
If so, then the code is injected into your robot then started.
So your image is saved to the NAO hard drive and I guess your robot doesn't have a folder named: "C:\Users\Claudia\Desktop\NAO\Bilder\test.png".
So change the path to "/home/nao/test.png", start your code, then log into your NAO using putty or browse folder using winscp (as it looks like you're using windows).
And you should see your image-file.
In order for your code to run correctly it needs to be properly indented. Your code should look like this:
import Image
def onInput_onStart(self):
cam_input = ALProxy("ALVideoDevice")
nameId = cam_input.subscribeCamera("Test_Cam", 1, 2, 13, 20)
image = cam_input.getImageRemote(nameId) #captures an image
w = image[0] #get the image width
h = image[1] #get the image height
pixel_array = image[6] #contains the image data
...
Make sure to indent everything that's inside the def onInput_onStart(self): method.
Sorry for the late response, but it maybe helpful for someone. You should try it with naoqi. Here is the documentation for retriving images
http://doc.aldebaran.com/2-4/dev/python/examples/vision/get_image.html
The original code was not working for me so I made some tweeks.
parser = argparse.ArgumentParser()
parser.add_argument("--ip", type=str, default="nao.local.",
help="Robot IP address. On robot or Local Naoqi: use
'nao.local.'.")
parser.add_argument("--port", type=int, default=9559,
help="Naoqi port number")
args = parser.parse_args()
session = qi.Session()
try:
session.connect("tcp://" + args.ip + ":" + str(args.port))
except RuntimeError:
pass
"""
First get an image, then show it on the screen with PIL.
"""
# Get the service ALVideoDevice.
video_service = session.service("ALVideoDevice")
resolution = 2 # VGA
colorSpace = 11 # RGB
videoClient = video_service.subscribe("python_client",0,3,13,1)
t0 = time.time()
# Get a camera image.
# image[6] contains the image data passed as an array of ASCII chars.
naoImage = video_service.getImageRemote(videoClient)
t1 = time.time()
# Time the image transfer.
print ("acquisition delay ", t1 - t0)
#video_service.unsubscribe(videoClient)
# Now we work with the image returned and save it as a PNG using ImageDraw
# package.
# Get the image size and pixel array.
imageWidth = naoImage[0]
imageHeight = naoImage[1]
array = naoImage[6]
image_string = str(bytearray(array))
# Create a PIL Image from our pixel array.
im = Image.fromstring("RGB", (imageWidth, imageHeight), image_string)
# Save the image.
im.save("C:\\Users\\Lenovo\\Desktop\\PROJEKTI\\python2-
connect4\\camImage.png", "PNG")
Be careful to use Python 2.7.
The code runs on your computer not the NAO robot!

How to change Bitmap1 for ToolBarToolBase Object in wxPython

How does one change the "image" shown on a toolbar button dynamically in wxPython?
frame = wx.Frame( ... )
tb = frame.CreateToolBar()
tool_bmp = wx.Bitmap("/path/to/tool.png", wx.BITMAP_TYPE_PNG)
tb.AddLabelTool(id=wx.ID_ANY, label="Clicky", bitmap=tool_bmp, bmpDisabled=wx.NullBitmap, shortHelp="Clicky")
tbtb = tb.GetToolByPos(0)
Specifically, I want to change the "image" shown on the ToolBarToolBase object tbtb. I have tried things like:
new_bmp = wx.Bitmap("/path/to/new.png", wx.BITMAP_TYPE_PNG)
tbtb.SetBitmap1(new_bmp)
tb.Refresh()
and
tool_bmp = wx.BitMap("/path/to/new.png, sx.BITMAP_TYPE_PNG)
tb.Refresh()
to no avail.
Try using SetNormalBitmap instead
new_bmp = wx.Bitmap("/path/to/new.png", wx.BITMAP_TYPE_PNG)
tbtb.SetNormalBitmap(new_bmp)
tb.Realize()
tb.Refresh()

Categories