import threading
import halcon as ha
from PIL import Image
from nicegui import ui, app, run
# from niceguiToolkit.layout import inject_layout_tool
# inject_layout_tool()
class Hal2Pil():
def __init__(self):
print("Hal2Pil")
self.acq = ha.open_framegrabber('DirectShow', 1, 1, 0, 0,
0, 0, 'default', 8, 'rgb', -1,
'false', 'default', '[0] ASUS 5M WebCam', 0, -1)
ha.grab_image_async(self.acq, 1)
self.imagePil = self.grab7convert()
self.thrEnable = True
self.start()
def grab7convert(self):
imageha = ha.grab_image(self.acq)
imageha=ha.zoom_image_factor(imageha,0.1,0.1,"constant")
arr = ha.himage_as_numpy_array(imageha)
return Image.fromarray(arr)
def __del__(self):
ha.close_framegrabber(self.acq)
def run(self):
while self.thrEnable:
# print("run")
self.imagePil = self.grab7convert()
def retpil(self):
return self.imagePil
def start(self):
threading.Thread(target=self.run).start()
h2p = Hal2Pil()
ucar = ui.card().style("width:1280;height:960")
ucar
with ucar:
img = h2p.retpil()
uiim = ui.interactive_image(img).style("width:800;height:600")
def fresh():
img = h2p.retpil()
uiim.set_source(img)
ui.timer(0.1,callback=fresh)
# app.shutdown(ha.close_framegrabber(acq))
ui.run(host='0.0.0.0', port=8080, show=True, reload=False) #, window_size=(800, 600)