def run(self): """ Always check if there is a queue of requests need to be processed. """ while self.switch=="on": # No requests if self.up_queue1.is_empty() and self.down_queue1.is_empty(): self.update_level_and_time(increment = 0, wait_time = 1) time.sleep(1) # print("The elevator is waiting and the time now is ", self.time_now) continue # Initialize a temporary queue for the elevator to move from the floor it stops at to the beginning floor of the next queue temp = myqueue() if not self.up_queue1.is_empty(): # If the elevator needs to move downward first to reach the first floor in its upward queue if self.current_floor > self.up_queue1.next()[0]: temp.insert([self.up_queue1.next()[0]]) self.direction = "transition_up" self.handle_queue(temp, -1) self.direction = "up" self.handle_queue(self.up_queue1, 1) if not self.down_queue1.is_empty(): # If the elevator needs to move upward first to reach the first floor in its downward queue if self.current_floor < self.down_queue1.next()[0]: temp.insert([self.down_queue1.next()[0]]) self.direction = "transition_down" self.handle_queue(temp,+1) self.direction = "down" self.handle_queue(self.down_queue1, -1)
def __init__(self, i, parent=None): super(elevator, self).__init__() self.ui = Ui_MainWindow() self.id = i self.status = 'wait' self.cur_layer = 1 self.queue = myqueue()
def __init__(self,starting_floor, max_capacity, max_speed, actions_log_file_path): self.current_floor = starting_floor self.max_capacity = max_capacity self.max_speed = max_speed self.actions_log_file_write = open(actions_log_file_path, "w") self.direction = "up" self.up_queue1 = myqueue() self.down_queue1 = myqueue(True) self.up_queue2 = myqueue() self.down_queue2 = myqueue(True) self.population = 0 self.customer_inside = [] self.time_now = 0 self.increment = 0 self.message = "" self.switch = "on"
def main(): fullbody_cascade = cv2.CascadeClassifier( '/Users/Aaron/Documents/College/Fourth_Year/Final_Year_Project/Datasets/body10/haarcascade_fullbody.xml' ) ap = argparse.ArgumentParser() ap.add_argument("-v", "--video", help="path to the video file") ap.add_argument("-a", "--min-area", type=int, default=500, help="minimum area size") args = vars(ap.parse_args()) if args.get("video", None) is None: camera = cv2.VideoCapture(0) # otherwise, we are reading from a video file else: print("[INFO] starting video file thread...") camera = myqueue(args["video"]).start() time.sleep(1.0) fps = frames().start() while camera.more(): frame = camera.read() frame = imutils.resize(frame, width=400) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cv2.putText(frame, "Queue Size: {}".format(camera.Q.qsize()), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2) fullbody = fullbody_cascade.detectMultiScale(gray, 1.3, 5) for (x, y, w, h) in fullbody: cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) roi_gray = gray[y:y + h, x:x + w] roi_color = frame[y:y + h, x:x + w] peds_found = "Found " + str(len(fullbody)) + " Pedestrians" cv2.putText(frame, peds_found, (10, 55), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2) cv2.imshow("HaarCascade", frame) cv2.waitKey(1) fps.update() k = cv2.waitKey(27) & 0xff if k == 27: break fps.stop() print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) cv2.destroyAllWindows() camera.stop()
def __init__(self): super().__init__() self.setupUi(self) ### 定义属性 self.user_id = '' self.message_que = myqueue.myqueue(10) self.text = "" self.chatting = False ### 设定初始界面属性 self.send_Button.setDisabled(True) self.friends_Button.setDisabled(True) ###信号槽绑定 self.send_Button.clicked.connect(self.send_mssage) self.sign_Button.clicked.connect(self.sign) self.friends_Button.clicked.connect(self.connect_friend) self.recv_signal.connect(self.add_recv_msg)
def handle_queue(self, queue, increment): """ Elevator moves up/down and let customers come in/out according to the queue. Parameters ---------- queue: myqueue A queue of customer requests. increment: int 1 for moving up, -1 for moving down. """ # If emergency stop is pressed, stop the program if self.switch != "on": return print ("Elevator now on floor ",self.current_floor, " and time is ", self.time_now) self.move_message() # Initialize a list for floors that may encounter overloading issue. overload_floors = [] while not queue.is_empty(): # If emergency stop is pressed, stop the program if self.switch != "on": return print ("The next floor to be visited in the queue is = ", queue.next()[0]) print ("The queue is = ",queue.queue) # move up/down to reach the next floor request in the queue while self.current_floor != queue.next()[0] and self.switch == "on": print ("elevator now on floor ",self.current_floor, " and time is ", self.time_now) self.move_message() time.sleep(1) self.update_level_and_time(increment) print ("elevator arrives at floor ",self.current_floor, " and time is ", self.time_now) """ When the customer sent the request, a list of 3 elements including the floor that the customer is at, the customer id, and the destiny floor. However, the information of the destiny floor will not be processed until the elevator reaches the floor the customer is at to pick up the customer. Once the elevator picks up the customer, it will pop the list of 3 elements and insert a list of 2 elements that only include the destiny floor and the customer ID. So we pop and insert elements from the queue according to different situations. One other situation is that, the elevator is empty and it's moving up/down because it's going to execute the next available queue, in this case the request does not come from the customer, so it will only contain 1 element, which is the floor the elevator is moving to. """ # 1 element if len(queue.next()) == 1 and self.switch == "on": queue.pop_visited() # 2 elements elif len(queue.next()) == 2 and self.switch == "on": self.population -= 1 self.customer_inside.remove(queue.next()[1]) self.message = "Customer " + str(queue.next()[1]) + " on floor " + str(queue.next()[0]) + " gets off the elevator. \n" self.log_actions() print (self.message) queue.pop_visited() # 3 elements else: if self.switch != "on": return # Before letting the customer enter inside the elevator, check if it will cause the overloading issue. if self.population < self.max_capacity: self.population += 1 customer_now, goto_floor = queue.next()[1:] self.customer_inside.append(customer_now) self.message = "Customer " + str(queue.next()[1]) + " on floor " + str(queue.next()[0]) + " get on the elevator. \n" self.log_actions() print (self.message) queue.pop_visited() queue.insert([goto_floor,customer_now]) else: print ("OVERLOAD") self.overload_message() # If the elevator is already full, remove the request from the current queue, and save it to a temporary list. # Repeat the process until someone get off the elevator. (request with only 2 elements) while len(queue.next()) != 2: overload_floors.append(queue.next()) self.message = "Customer " + str(queue.next()[1]) + " on floor " + str(queue.next()[0]) + " need to wait for the next elevator. \n" self.log_actions() print (self.message) queue.pop_visited() print ("Customers inside the elevator: ", self.customer_inside) # If emergency stop is pressed, stop the program if self.switch != "on": return # Update all 4 queues if queue == self.up_queue1: self.up_queue2.combine(overload_floors) self.up_queue1 = self.up_queue2 self.up_queue2 = myqueue() elif queue == self.down_queue1 and self.down_queue2: self.down_queue2.combine(overload_floors) self.down_queue1 = self.down_queue2 self.down_queue2 = myqueue(True)
def communicate(): client,addr,user_id = clients.deque() #print('find connection at address: '+ str(addr)) print(user_id,' signed in') chatting = False friend_id = '' while True: ### 与服务器沟通测试连接情况以及配置功能 if not chatting: # while循环实现阻塞 recv_flag = False while not recv_flag: try: msg = client.recv(1024).decode() recv_flag = True except TimeoutError: pass # 根据收到的msg判断是否是要查找好友 friend_match = re.match("....\d{4}....", msg) if friend_match: friend_id = re.findall('\d+', friend_match.string)[0] if friend_id in client_info and friend_id != user_id: # 告诉客户端正式会话开始 print('find friend id: ', friend_id) client.send('1'.encode()) chatting = True else: client.send('0'.encode()) # 正常的连接测试 else: if msg == 'byby': client.close() runing_clients.remove(user_id) #print(str(addr) + 'closed') print(user_id, ' signed out') break else: words = 'You said:%s' % msg client.send(words.encode()) ### 开始会话 else: # 消息队列不为空时,将消息发给客户端 if user_id in unread_msg and unread_msg[user_id].length>0: for i in range(unread_msg[user_id].length): mssg = unread_msg[user_id].deque() client.send(mssg.encode()) try: # 接受信息 msg = client.recv(1024).decode() # 消息入缓存队列 if friend_id not in unread_msg: unread_msg[friend_id] = myqueue.myqueue(10) unread_msg[friend_id].enque(msg) # 说了再见 if 'byby' in msg: client.close() runing_clients.remove(user_id) print(user_id, ' signed out') break # 接受信息超时(未收到信息) except TimeoutError: pass
client_info = dict() ### 保存沟通记录,字典存储,关机时存到文件 用户id相连:内容字符串 chat_countent = dict() ### 消息缓存,字典存储,键是用户id,值是消息队列(最长缓存200条信息) #当消息队列不为空且用户在线时,将消息队列逐条发给对应的用户 # 关机时若消息队列不为空,则存入文件,开机时读入 unread_msg = dict() ### 记录已登录的用户id runing_clients = set() ### 建立socket并监听 max_connection = 5 # 最大连接个数 clients = myqueue.myqueue(max_connection) sockt = socket.socket() # 创建 socket 对象 host = socket.gethostname() # 获取本地主机名 port = 1234 # 设置端口 sockt.bind((host, port)) # 绑定端口 sockt.listen(max_connection) # 等待客户端连接 # 设置1s超时 val = struct.pack("Q", 1000) sockt.setsockopt(socket.SOL_SOCKET, socket.SO_RCVTIMEO, val) get_connect = threading.Thread(target=find_connection) get_connect.start() # print('current thread count: ',threading.active_count()) while True: thread_count = threading.active_count()
import datetime import imutils import cv2 import time ap = argparse.ArgumentParser() ap.add_argument("-v", "--video", help="path to the video file") ap.add_argument("-a", "--min-area", type=int, default=500, help="minimum area size") args = vars(ap.parse_args()) if args.get("video", None) is None: camera = cv2.VideoCapture(0) # otherwise, we are reading from a video file else: print("[INFO] starting video file thread...") camera = myqueue(args["video"]).start() time.sleep(1.0) i = 0 centerX = 0 centerY = 0 objList = [] meas = [] pred = [] mp = np.array((2, 1), np.float32) # measurement tp = np.zeros((2, 1), np.float32) # tracked / prediction kalman = cv2.KalmanFilter(4, 2) kalman.measurementMatrix = np.array([[1, 0, 0, 0], [0, 1, 0, 0]], np.float32) kalman.transitionMatrix = np.array([[1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0], [0, 0, 0, 1]], np.float32) kalman.processNoiseCov = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], np.float32) * 0.03