Kinect shuts down after running this code and gives error - python

I'm trying to run the following code,
from pykinect import nui
from pykinect.nui import JointId
from pykinect.nui import SkeletonTrackingState
from pykinect.nui import TransformSmoothParameters
with nui.Runtime() as kinect:
kinect.skeleton_frame_ready+=skeleton_frame_ready
kinect.skeleton_engine.enable=True
while True:
frame= kinect.skeleton_engine.get_next_frame()
for skeleton in frame.SkeletonData:
def skeleton_fram_raedy(skeleton_frame):
for index,data in enumerate(skeleton):
if skeleton.eTrackingState==nui.SkeletonTrackingState.TRACKED:
head=data.SkeletonPositions[JointId.Head]
print head
but Kinect shuts down and gives the following error:
Traceback (most recent call last):
File "C:\Users\sayyed javed ahmed\Desktop\Humaira\Practice Codes-Python\skeletonnew.py", line 5, in <module>
with nui.Runtime() as kinect:
File "C:\Python27\lib\site-packages\pykinect\nui\__init__.py", line 126, in __init__
self.camera = Camera(self)
File "C:\Python27\lib\site-packages\pykinect\nui\__init__.py", line 352, in __init__
self.elevation_angle
File "C:\Python27\lib\site-packages\pykinect\nui\__init__.py", line 359, in get_elevation_angle
return self.runtime._nui.NuiCameraElevationGetAngle()
File "C:\Python27\lib\site-packages\pykinect\nui\_interop.py", line 200, in NuiCameraElevationGetAngle
_NuiInstance._NuiCameraElevationGetAngle(self, ctypes.byref(res))
File "_ctypes/callproc.c", line 950, in GetResult
WindowsError: [Error -2147024883] The data is invalid
I'm new to PyKinect so any help is appreciated! Thanks!

Firstly, there are typos in your code. You're assigning a callback to skeleton_frame_ready function in kinect.skeleton_frame_ready+=skeleton_frame_ready, but have defined it as def skeleton_fram_raedy(skeleton_frame).
Secondly, I think your indentation is off, you have a function definition in a for loop in a while loop:
while True:
frame= kinect.skeleton_engine.get_next_frame()
for skeleton in frame.SkeletonData:
def skeleton_fram_raedy(skeleton_frame):
...
Try something like this:
from pykinect import nui
from pykinect.nui import JointId
from pykinect.nui import SkeletonTrackingState
from pykinect.nui import TransformSmoothParameters
def skeleton_frame_ready(skeleton_frame):
for skeleton in frame.SkeletonData:
if skeleton.eTrackingState==nui.SkeletonTrackingState.TRACKED:
for index,data in enumerate(skeleton):
head=data.SkeletonPositions[JointId.Head]
print head
with nui.Runtime() as kinect:
kinect.skeleton_frame_ready+=skeleton_frame_ready
kinect.skeleton_engine.enable=True
while True:
frame= kinect.skeleton_engine.get_next_frame()
Thirdly, the error indicates that invalid argument is passed to nui for the kinect camera angle. Not sure why that is. You could try setting the elevation angle to 0 right after initializing nui:
with nui.Runtime() as kinect:
kinect.camera.elevation_angle = 0
kinect.skeleton_frame_ready+=skeleton_frame_ready

Related

Cant locate image on pyautogui

Hey i am trying to locate an image but get this error on why i use the value as (any,any,250,250) but doesnt show the error when i use(any,any,350(more than 350),250(more than 250))
here is my code
`
from pyautogui import *
import pyautogui
import time
import keyboard
import random
import win32api, win32con
import cv2
import numpy as np
PlayerOnRight = False
PlayerOnFullRight = False
PlayerOnFullLeft = False
PlayerOnLeft = False
PlayerOnMiddle = False
while 1:
if PlayerOnMiddle == True and pyautogui.locateOnScreen('player.png', region=(1080,600,800,200), confidence=0.7) != None:
print("Right Side")
time.sleep(0.5)
if pyautogui.locateOnScreen('player.png', region=(825,800,250,250), confidence=0.3) !=None:
print("Left Side")
time.sleep(0.5)
`
here is the error
`Traceback (most recent call last):
File "C:\Users\Admin\Desktop\Pyhton_bot\test.py", line 20, in <module>
if pyautogui.locateOnScreen('player.png', region=(825,800,250,250), confidence=0.3) !=None:
File "C:\Python310\lib\site-packages\pyautogui\__init__.py", line 175, in wrapper
return wrappedFunction(*args, **kwargs)
File "C:\Python310\lib\site-packages\pyautogui\__init__.py", line 213, in locateOnScreen
return pyscreeze.locateOnScreen(*args, **kwargs)
File "C:\Python310\lib\site-packages\pyscreeze\__init__.py", line 373, in locateOnScreen
retVal = locate(image, screenshotIm, **kwargs)
File "C:\Python310\lib\site-packages\pyscreeze\__init__.py", line 353, in locate
points = tuple(locateAll(needleImage, haystackImage, **kwargs))
File "C:\Python310\lib\site-packages\pyscreeze\__init__.py", line 219, in _locateAll_opencv
raise ValueError('needle dimension(s) exceed the haystack image or region dimensions')
ValueError: needle dimension(s) exceed the haystack image or region dimensions
`
i expted it to work noramlly to locate image and pint the given text but got this error
please help me fix this
Because the region has to be bigger than your player.png image. Either crop your player.png image or increase the region size

Bot crashing from pyautogui

I am making a bot that makes the chrome dinosaur jump every time it sees a cactus.
The RGB for the cactus is (83,83,83)
I made a specific coordinate for my code to look at, and check if the RGB of that pixel matches the cactus (jump_place)
However, when I run it, it works for about 2 seconds, and then gives me this error:
Traceback (most recent call last):
File "c:\Users\dofia\OneDrive\Desktop\coding VS\Other Projects\DINO BOT\tempCodeRunnerFile.py", line 29, in <module>
if pyautogui.pixel(810, 635)[0] == 83:
File "C:\Users\dofia\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pyscreeze\__init__.py", line 584, in pixel
return (r, g, b)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.496.0_x64__qbz5n2kfra8p0\lib\contextlib.py", line 124, in __exit__
next(self.gen)
File "C:\Users\dofia\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pyscreeze\__init__.py", line 113, in __win32_openDC
raise WindowsError("windll.user32.ReleaseDC failed : return 0")
OSError: windll.user32.ReleaseDC failed : return 0
Here is my code:
from pyautogui import *
import pyautogui
import time
import keyboard
import random
time.sleep(3)
replay = (1127,591)
jump_place = (866,630)
def restart():
pyautogui.click(replay)
def jump():
pyautogui.keyDown('space')
time.sleep(0.5)
print("Jump!")
pyautogui.keyUp('space')
restart()
while keyboard.is_pressed('p') == False:
if pyautogui.pixel(810, 635)[0] == 83:
jump()
Is there anything I am doing wrong?
The link to the dinosaur game is: http://www.trex-game.skipser.com/

How to assign a value to a sliced output signal?

I'm a beginner with myhdl.
I try to translate the following Verilog code to MyHDL:
module ModuleA(data_in, data_out, clk);
input data_in;
output reg data_out;
input clk;
always #(posedge clk) begin
data_out <= data_in;
end
endmodule
module ModuleB(data_in, data_out, clk);
input [1:0] data_in;
output [1:0] data_out;
input clk;
ModuleA instance1(data_in[0], data_out[0], clk);
ModuleA instance2(data_in[1], data_out[1], clk);
endmodule
Currently, I have this code:
import myhdl
#myhdl.block
def ModuleA(data_in, data_out, clk):
#myhdl.always(clk.posedge)
def logic():
data_out.next = data_in
return myhdl.instances()
#myhdl.block
def ModuleB(data_in, data_out, clk):
instance1 = ModuleA(data_in(0), data_out(0), clk)
instance2 = ModuleA(data_in(1), data_out(1), clk)
return myhdl.instances()
# Create signals
data_in = myhdl.Signal(myhdl.intbv()[2:])
data_out = myhdl.Signal(myhdl.intbv()[2:])
clk = myhdl.Signal(bool())
# Instantiate the DUT
dut = ModuleB(data_in, data_out, clk)
# Convert tfe DUT to Verilog
dut.convert()
But it doesn't works because signal slicing produce a read-only shadow signal (cf MEP-105).
So, what is it the good way in MyHDL to have a writable slice of a signal?
Edit:
This is the error I get
$ python demo.py
Traceback (most recent call last):
File "demo.py", line 29, in <module>
dut.convert()
File "/home/killruana/.local/share/virtualenvs/myhdl_sandbox-dYpBu4o5/lib/python3.6/site-packages/myhdl-0.10-py3.6.egg/myhdl/_block.py", line 342, in convert
File "/home/killruana/.local/share/virtualenvs/myhdl_sandbox-dYpBu4o5/lib/python3.6/site-packages/myhdl-0.10-py3.6.egg/myhdl/conversion/_toVerilog.py", line 177, in __call__
File "/home/killruana/.local/share/virtualenvs/myhdl_sandbox-dYpBu4o5/lib/python3.6/site-packages/myhdl-0.10-py3.6.egg/myhdl/conversion/_analyze.py", line 170, in _analyzeGens
File "/usr/lib/python3.6/ast.py", line 253, in visit
return visitor(node)
File "/home/killruana/.local/share/virtualenvs/myhdl_sandbox-dYpBu4o5/lib/python3.6/site-packages/myhdl-0.10-py3.6.egg/myhdl/conversion/_analyze.py", line 1072, in visit_Module
File "/home/killruana/.local/share/virtualenvs/myhdl_sandbox-dYpBu4o5/lib/python3.6/site-packages/myhdl-0.10-py3.6.egg/myhdl/conversion/_misc.py", line 148, in raiseError
myhdl.ConversionError: in file demo.py, line 4:
Signal has multiple drivers: data_out
You can use an intermediate list of Signal(bool()) as placeholder.
#myhdl.block
def ModuleB(data_in, data_out, clk):
tsig = [myhdl.Signal(bool(0)) for _ in range(len(data_in))]
instances = []
for i in range(len(data_in)):
instances.append(ModuleA(data_in(i), tsig[i], clk))
#myhdl.always_comb
def assign():
for i in range(len(data_out)):
data_out.next[i] = tsig[i]
return myhdl.instances()
A quick (probably non-fulfilling) comment, is that the intbv is treated as a single entity that can't have multiple drives. Two references that might help shed some light:
http://jandecaluwe.com/hdldesign/counting.html
http://docs.myhdl.org/en/stable/manual/structure.html#converting-between-lists-of-signals-and-bit-vectors

Pyglet and PIL: glError

I am wondering why I can get this error:
pyglet.lib.gl.GLExeption: invalid value
at: self.group = TextureGroup(image.load(TEXTURE_PATH).get_texture())
Imported libs:
import sys
import math
import random
import time
from collections import deque
from pyglet import image
from pyglet.gl import *
from pyglet.graphics import TextureGroup
from pyglet.window import key, mouse, Window
from PIL import Image
I'm using PIL for making the PNG:
print "Creating ", TEXTURE_PATH
images = map(Image.open, texture_path)
widths, heights = zip(*(i.size for i in images))
total_width = sum(widths)
max_height = max(heights)
new_im = Image.new('RGB', (total_width, max_height))
x_offset = 0
for im in images:
new_im.paste(im, (x_offset,0))
x_offset += im.size[0]
new_im.convert('RGB')
new_im.save(TEXTURE_PATH, "PNG")
And the 'texture_path`(lowercase) is here:
texture_path = ['res/textures/grass_top.png'] * 99
The full error message is:
Traceback (most recent call last):
File "D:\pythoncode\pyglet\minecraft_remake.py", line 955, in <module>
main()
File "D:\pythoncode\pyglet\minecraft_remake.py", line 948, in main
window = Window(width = 640, height = 480, caption="Minecraft 3d!", resizable=True)
File "D:\pythoncode\pyglet\minecraft_remake.py", line 554, in __init__
self.model = Model()
File "D:\pythoncode\pyglet\minecraft_remake.py", line 198, in __init__
self.group = TextureGroup(image.load(TEXTURE_PATH).get_texture())
File "C:\Python27\lib\site-packages\pyglet\image\__init__.py", line 818, in get_texture
force_rectangle)
File "C:\Python27\lib\site-packages\pyglet\image\__init__.py", line 803, in create_texture
rectangle, force_rectangle)
File "C:\Python27\lib\site-packages\pyglet\image\__init__.py", line 1514, in create
blank)
File "C:\Python27\lib\site-packages\pyglet\gl\lib.py", line 104, in errcheck
raise GLException(msg)
GLException: invalid value
I found the problem, but didn't know how to fix it, the problem are in the PNG making.
When I replace the textures.png with another picture, it's working
but when I make one with PIL, it doesn't work.
Solved: the problem is in my .PNG Editor, i just make the image manually and it works!
im using Graphics Gale Free edition to edit and it will work.
i think the problem is in the PIL itself. Or The pyglet doesnt load PIL config PNG
P.s it may be because color configuration

K means clustering using weka python

from weka.clusterers import Clusterer
import weka.core.converters as converters
data = converters.load_any_file("/home/ubuntu/test.csv")
data.class_is_last()
clusterer = Clusterer(classname="weka.clusterers.SimpleKMeans", options=["-N", "3"])
clusterer.build_clusterer(data)
print(clusterer)
# cluster the data
for inst in data:
cl = clusterer.cluster_instance(inst) # 0-based cluster index
dist = clusterer.distribution_for_instance(inst) # cluster membership distribution
print("cluster=" + str(cl) + ", distribution=" + str(dist))
I used the above code for doing k means custering i am not able to execute the program
The following are the errors I get
Traceback (most recent call last):
File "clus.py", line 6, in <module>
data = converters.load_any_file("/home/ubuntu/hello.csv")
File "/usr/local/lib/python2.7/dist-packages/weka/core/converters.py", line 255, in load_any_file
loader = loader_for_file(filename)
File "/usr/local/lib/python2.7/dist-packages/weka/core/converters.py", line 239, in loader_for_file
"(Ljava/lang/String;)Lweka/core/converters/AbstractFileLoader;", filename)
File "/usr/local/lib/python2.7/dist-packages/javabridge/jutil.py", line 932, in static_call
fn = make_static_call(class_name, method_name, sig)
File "/usr/local/lib/python2.7/dist-packages/javabridge/jutil.py", line 903, in make_static_call
klass = env.find_class(class_name)
AttributeError: 'NoneType' object has no attribute 'find_class'
I don't know why I am getting these errors. Can someone help me with this?
As described in the python-weka-wrapper API you have to import and start the Java Virtual Machine:
import weka.core.jvm as jvm
jvm.start()
It should solve your problem.

Categories