AttributeError: 'Image' object has no attribute 'frame' - python

I am trying to save images from Carla in my disk, but I receive this error on the terminal.
Traceback (most recent call last):
File "carla_basic_tutorial.py", line 83, in <lambda>
'out%02d/%06d.png' % (n_output, image.frame)
AttributeError: 'Image' object has no attribute 'frame'
I have already installed the libraries from requirements files and Pillow too, the drivers of GPU were installed.
The part of code is available below
# Spawn the camera and attach it to the vehicle
camera = world.spawn_actor(
camera_bp,
camera_transform,
attach_to=vehicle
)
actor_list.append(camera)
print('created %s' % camera.type_id)
# Check how much "out" folders already exists
n_output = len([d for d in os.listdir() if d.startswith('out')])
# Sets the function that will be called by the camera
# This will save the images to disk at a "out" folder
camera.listen(lambda image: image.save_to_disk(
'out%02d/%06d.png' % (n_output, image.frame)
))
The full code is available here Full code

I solved this using the attribute image.frame_number
camera.listen(lambda image: image.save_to_disk(
'out%02d/%06d' % (n_output, image.frame_number)
))

Related

File on Google Drive doesn't appear in Colab

I'm learning to use OpenCV on Colab to deal with object tracking in videos.
I have a sample .mp4 video called low den.mp4 that I uploaded "indirectly" to my own gdrive. So that I can mount it by:
import cv2
from google.colab import drive
drive.mount('/content/gdrive')
then I can run the cell successfully by
path = "/gdrive/My Drive/low den.mp4"
But when I call
video = cv2.VideoCapture(path)
if not video.isOpened():
print('Error while loading the video!')
sys.exit()
Colab tells me this:
Error while loading the video!
An exception has occurred, use %tb to see the full traceback.
SystemExit
I tried the "direct" method mentioned elsewhere:
from google.colab import files
uploaded = files.upload()
Video upload alright. Then if I call
video = cv2.VideoCapture(uploaded)
if not video.isOpened():
print('Error while loading the video!')
sys.exit()
Colab tells me this:
error Traceback (most recent call
last)
<ipython-input-39-b5c3939fe4ac> in <module>
----> 1 video = cv2.VideoCapture(uploaded)
2 if not video.isOpened():
3 print('Error while loading the video!')
4 sys.exit()
error: OpenCV(4.6.0) :-1: error: (-5:Bad argument) in function
'VideoCapture'
> Overload resolution failed:
> - Can't convert object to 'str' for 'filename'
> - VideoCapture() missing required argument 'apiPreference' (pos 2)
> - Argument 'index' is required to be an integer
> - VideoCapture() missing required argument 'apiPreference' (pos 2)
I need some help on understanding what went wrong in the "indirect" vs "direct" methods. And for the "direct" dialogue upload, how do I convert the uploaded video object to a file path and use it for other cv functions?
Thank you

When I am trying to get live stream from youtube using opencv and camgear I am getting the following error

I want to get the live stream from youtube, and for that, I have used opencv along with the package vidgear. But while running the code, I am getting the following error. I am sure that there is no problem with the URL.
I have tried with pafy and streamlink. Even though both have given the result but after few frames, it was getting stuck and I want sequential frames without any pause.
import cv2
from vidgear.gears import CamGear
stream = CamGear(source="https://www.youtube.com/watch?v=VIk_6OuYkSo", y_tube =True, time_delay=1, logging=True).start() # YouTube Video URL as input
while True:
frame = stream.read()
if frame is None:
break
cv2.imshow("Output Frame", frame)
key = cv2.waitKey(30)
if key == ord("q"):
break
cv2.destroyAllWindows()
stream.stop()
Error output ::
'NoneType' object has no attribute 'extension'
Traceback (most recent call last):
File "C:\Users\CamfyVision\AppData\Local\Programs\Python\Python36\lib\site-packages\vidgear\gears\camgear.py", line 120, in __init__
print('Extension: {}'.format(_source.extension))
AttributeError: 'NoneType' object has no attribute 'extension'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "DronrStream.py", line 4, in <module>
stream = CamGear(source="https://www.youtube.com/watch?v=VIk_6OuYkSo", y_tube =True, time_delay=1, logging=True).start() # YouTube Video URL as input
File "C:\Users\CamfyVision\AppData\Local\Programs\Python\Python36\lib\site-packages\vidgear\gears\camgear.py", line 125, in __init__
raise ValueError('YouTube Mode is enabled and the input YouTube Url is invalid!')
ValueError: YouTube Mode is enabled and the input YouTube Url is invalid!
#Adithya Raj I'm the author of VidGear Video Processing python library.
This error is because of a bug with YouTube live streams and is already been resolved in this commit. Kindly update vidgear as follows:
pip install -U vidgear
i was try to run your script and i got error in URL link but when i replace with other URL link then you're script works fine no error. have look pics

Getting "'NoneType' object has no attribute 'items'" error with PIL library

Code:
#!/usr/bin/python3
from PIL import Image, ExifTags
img = Image.open("/root/Bilder/sysPics/schwarz-weiß-Karte.jpg")
for i, j in img._getexif().items():
if i in ExifTags.TAGS:
print(ExifTags.TAGS[i] + " - " + str(j))
Error:
Traceback (most recent call last):
File "python-tool.py", line 7, in <module>
for i, j in img._getexif().items():
AttributeError: 'NoneType' object has no attribute 'items'
Can anyone help me? I never worked with the PIL lib but I saw a tutorial,
in which this .items() method worked:
https://www.youtube.com/watch?v=-PiR5SX4Mxo&list=PLNmsVeXQZj7onbtIXvxZTzeKGnzI6NFp_&index=8
There is no difference between his code and mine, I can't believe they cut off the .items() method in the last patches.
1st, you should not be relying on internal functions such as _get_exif() because their implementation can change at any time and they are usually not meant for public use. (See _single_leading_underscore from PEP8 naming conventions).
2nd, you should consider that not all images have EXIF data. It’s possible that trying to get the EXIF data will None. So it's not the .items() method that's the problem, rather it's that your _get_exif() returned None. Your code does not have handling for that case, you are always assuming _get_exif() returns a dict.
Now, to solve your problem, for Python3.x (I have Python3.6.8) and PIL (installed as Pillow, Pillow==6.0.0), the Image object now provides a public getexif() method that you can use. The return type is None if the image has no EXIF data or a <class 'PIL.Image.Exif'>.
from PIL import Image, ExifTags
img = Image.open("sample.jpg")
print(dir(img))
# [... 'getexif' ...]
img_exif = img.getexif()
if img_exif:
print(type(img_exif))
# <class 'PIL.Image.Exif'>
print(dict(img_exif))
# { .. 271: 'FUJIFILM', 305: 'Adobe Photoshop Lightroom 6.14 (Macintosh)', }
img_exif_dict = dict(img_exif)
for key, val in img_exif_dict.items():
if key in ExifTags.TAGS:
print(ExifTags.TAGS[key] + " - " + str(val))
else:
print("Sorry, image has no exif data.")

BGE Error: 'VideoTexture.Texture' object has no attribute 'materialID'

I tried to change the texture of an object in the blender game engine.
I thought it would be a good idea to use the Texture Replacement from the Video Texture (bge.texture).
I tried to run the following script:
def createTexture(cont):
obj = cont.owner
# get the reference pointer (ID) of the internal texture
ID = texture.materialID(obj, 'Kraftwerk2.png')
# create a texture object
object_texture = texture.Texture(obj, ID)
# create a new source with an external image
url = logic.expandPath(new_file)
new_source = texture.ImageFFmpeg(url)
# the texture has to be stored in a permanent Python object
logic.texture = object_texture
# update/replace the texture
logic.texture.source = new_source
logic.texture.refresh(False)
def removeTexture(cont):
"""Delete the Dynamic Texture, reversing back the final to its original state."""
try:
del logic.texture
except Exception as e:
print(e)
but it failed with the following error message:
Python script error - object 'Plane', controller 'Python': Traceback
(most recent call last): File
"F:\Benutzer\Merlin\MW-Industries\Blender
Dateien\Cinema\Render-Blend\MoonSpace.ble nd\Test.py", line 19, in
createTexture AttributeError: 'VideoTexture.Texture' object has no
attribute 'materialID'
Is there a way to solve the problem?

How to save an Image locally using PIL

I am making a program that builds a thumbnail based on user input, and I am running into problems actually saving the image. I am used to C++ where you can simply save the image, it seems that python does not support this.
This is my code right now:
def combine(self):
img = Image.new("RGBA", (top.width,top.height+mid.height+mid.height),(255,255,255))
img.paste(top, (0,0))
img.paste(mid, (0,top.height))
img.paste(bot, (0,top.height+mid.height))
img.show()
img.save("Thumbnail.png", "PNG")
The error that shows up when I run it is :
Traceback (most recent call last):
File "TextToThumbnail.py", line 4, in <module>
class Thumbnail(object):
File "TextToThumbnail.py", line 461, in Thumbnail
img.save("Thumbnail.png", "PNG")
NameError: name 'img' is not defined
What is going on? Preferably I just want to be able to save the image locally, since this program will be running on multiple setups with different pathways to the program.

Categories

Resources