def imreceive(self): arrayname = self.zmq_socket.recv_json() image_data = self.zmq_socket.recv(flags=0, copy=True, track=False) a = np.frombuffer(image_data, dtype=arrayname['dtype']) image = a.reshape(arrayname['shape']) save_log(f'Receiving ZMQ image in port {self.opo}') return arrayname['msg'], image
def ambilgambar(self): try: grabresult = self.camera.RetrieveResult( 5000, pylon.TimeoutHandling_ThrowException) # print("ambil gambar", grabResult.GrabSucceeded()) if grabresult.GrabSucceeded(): # Access the image data image = self.converter.Convert(grabresult) img = image.GetArray() # print(img.shape) img = cv2.flip(img, 1) save_log('Capture image SUCCESS') return img else: # return np.zeros((1024,1280,3)).astype(np.uint8), 0 while not grabresult.GrabSucceeded(): save_log('Capture image FAILED') grabresult = self.camera.RetrieveResult( 5000, pylon.TimeoutHandling_ThrowException) image = self.converter.Convert(grabresult) img = image.GetArray() img = cv2.flip(img, 1) return img # img = np.zeros((3,3,3)) # grabResult.Release() # camera.StopGrabbing() # Releasing the resource # return img except Exception as e: exit() print('gagal ambil gambar') print(e) return dummy, 0
def ambilgambar(self): files = os.listdir(self.sumberdata) imgfile = random.choice(files) save_log(f'{self.sumberdata + imgfile}') while not imgfile.endswith(".png"): imgfile = random.choice(files) print(self.sumberdata + imgfile) img = cv2.imread(self.sumberdata + imgfile) return img
def detection(img, sec, n): inf_time = time.time() ori, pic, stat_list, boxes = do_detect(img) # boxes dalam int save_log(f'Inference SUCCESS in {time.time() - inf_time}') if len(stat_list) < 1: pred = "OK" stat_list = ['OK'] boxes = [] pics = ['OK'] else: pred = "NG" pics = pic pkt = {"sec": sec} zmqo_save.imsend(pkt, ori) print(sec + pred + str(n)) zmqo.imsend(sec + pred + str(n), pic) return stat_list, boxes, pics
import threading from libraries.basler_cam import kamera from libraries import zmqimage from utils.Utils import get_plc_data, change_plc_data, connectPLC, save_log connectPLC() basler1 = kamera(ip_address='192.168.0.235') basler2 = kamera(ip_address='192.168.0.234') basler3 = kamera(ip_address='192.168.0.123') try: zmqo_images = zmqimage.ZmqConnect(connect_to="tcp://localhost:5678") save_log('Connected to processing script') except: save_log('Unable to connect to processing script') class CamCapture(threading.Thread): def run(self): intd = 0 while True: cam_status = get_plc_data('MR15') cam_pos_T1_1 = get_plc_data('MR5') cam_pos_T1_2 = get_plc_data('MR6') cam_pos_D26_1 = get_plc_data('MR9') cam_pos_D26_2 = get_plc_data('MR10') cam_pos_D78_1 = get_plc_data('MR11') cam_pos_D78_2 = get_plc_data('MR12') end_part = get_plc_data('MR301') if cam_status == '1':
def __init__(self, connect_to="tcp://localhost:5555"): self.context = zmq.Context() self.zmq_socket = self.context.socket(zmq.PUSH) self.zmq_socket.connect(connect_to) self.conto = connect_to save_log('Connecting to ZMQ server: ' + connect_to)
def string_receive(self): arrayname = self.zmq_socket.recv_json() save_log(f'Receiving ZMQ string in port {self.opo}') return arrayname
def __init__(self, open_port="tcp://*:5555"): self.context = zmq.Context() self.zmq_socket = self.context.socket(zmq.PULL) self.zmq_socket.bind(open_port) self.opo = open_port save_log(f'Opening ZMQ server: {open_port}')
def sendstring(self, arrayname): self.zmq_socket.send_json(arrayname) save_log(f'Send ZMQ string to {self.conto}')
def imsend(self, arrayname, array): md = self.conv_array(array, arrayname) self.zmq_socket.send_json(md) self.zmq_socket.send(array, flags=0, copy=True, track=False) save_log(f'Send ZMQ image to {self.conto}')
def run(self): warmup() change_plc_data('MR0', '1') slist1 = [] slist2 = [] slist3 = [] slist4 = [] slist5 = [] slist6 = [] blist1 = [] blist2 = [] blist3 = [] blist4 = [] blist5 = [] blist6 = [] ilist1 = [] ilist2 = [] ilist3 = [] ilist4 = [] ilist5 = [] ilist6 = [] i1 = 0 i2 = 0 D78 = get_plc_data('MR1709') D26 = get_plc_data('MR1509') while True: start = time.time() cam_pos1, img1 = zmqi_img.imreceive() cam_pos2, img2 = zmqi_img.imreceive() cam_pos3, img3 = zmqi_img.imreceive() save_log(f'Receive images SUCCESS: {time.time() - start}') if cam_pos1[0] == '1': i1 += 1 save_log(f'Processing type {cam_pos1[1]} rotation {i1}') mainprocess(slist1, slist2, slist3, blist1, blist2, blist3, ilist1, ilist2, ilist3, ["Sec1", "Sec2", "Sec3"], i1, img1, img2, img3) elif cam_pos1[0] == '2' and cam_pos1[1] == 'T1': i2 += 1 save_log(f'Processing type {cam_pos1[1]} rotation {i1}') mainprocess2(slist4, slist5, blist4, blist5, ilist4, ilist5, ["Sec4", "Sec5"], i2, img1, img2, img3) elif cam_pos1[0] == '2' and not cam_pos1[1] == 'T1': i2 += 1 save_log(f'Processing type {cam_pos1[1]} rotation {i1}') mainprocess(slist4, slist5, slist6, blist4, blist5, blist6, ilist4, ilist5, ilist6, ["Sec4", "Sec5", "Sec6"], i2, img1, img2, img3) if cam_pos1[0] == 'Done': save_log('Done processing one part') i1 = 0 i2 = 0 zmqo.imsend("Done", img1) if D26 == '1' or D78 == '1': save_log('Start validating data for type D26 / D78') slist_all = [ slist1, slist2, slist3, slist4, slist5, slist6 ] blist_all = [ blist1, blist2, blist3, blist4, blist5, blist6 ] ilist_all = [ ilist1, ilist2, ilist3, ilist4, ilist5, ilist6 ] NG_list, NG_num, NG_sect, NG_img = d78_decision( blist_all, slist_all, ilist_all) else: save_log('Start validating data for type D73 / D98') slist_all = [slist1, slist2, slist3, slist4, slist5] blist_all = [blist1, blist2, blist3, blist4, blist5] ilist_all = [ilist1, ilist2, ilist3, ilist4, ilist5] NG_list, NG_num, NG_sect, NG_img = final_decision( blist_all, slist_all, ilist_all) print("paralel waiting CPT FALSE... ") print("################\n", NG_list, "\n################") zmqo.imsend('Done', img2) # zmqo.imsend(NG_list, dummy) if len(NG_list) < 1: judgment = 'OK' print('OK') zmqo.imsend('OK', img3) change_plc_data('MR13', '1') else: print('NG1') judgment = 'NG: ' ngst = 'NG: ' for i in range(len(NG_list)): print('NG: {} sebanyak {} di section{}'.format( NG_list[i], NG_num[i], NG_sect[i])) ngst = ngst + ' ; ' + NG_list[ i] + " di section {}".format(NG_sect[i]) zmqo_aio_ng.imsend({ 'ng': NG_list, 'section': NG_sect }, NG_img[0]) zmqo.imsend(ngst, img3) print('NG2') change_plc_data('MR14', '1') zmqo_save.imsend(judgment, img1) slist1.clear() slist2.clear() slist3.clear() slist4.clear() slist5.clear() slist6.clear() blist1.clear() blist2.clear() blist3.clear() blist4.clear() blist5.clear() blist6.clear() ilist1.clear() ilist2.clear() ilist3.clear() ilist4.clear() ilist5.clear() ilist6.clear()