def test_face(img, face, face_feature): eyes, face_grid = face_feature if len(eyes) < 2: return None start_ms = current_time() transformed_right_eye = right_eye_transformer.preprocess( 'image_right', crop_image(img, eyes[0])) transformed_left_eye = left_eye_transformer.preprocess( 'image_left', crop_image(img, eyes[1])) transformed_face = face_transformer.preprocess('image_face', crop_image(img, face)) transformed_face_grid = face_grid.reshape(1, 625, 1, 1) net.blobs['image_left'].data[...] = transformed_left_eye net.blobs['image_right'].data[...] = transformed_right_eye net.blobs['image_face'].data[...] = transformed_face net.blobs['facegrid'].data[...] = transformed_face_grid output = net.forward() # net.forward() print("Feeding through the network took " + str((current_time() - start_ms) * 1. / 1000) + "s") return np.copy(output['fc3'][0])
def move_to(self, loc): """ Manage a new location - A real client needs to expire from locations.append if older than 45 days """ old_location = self.current_location if old_location: old_location["end_time"] = current_time() if (old_location["end_time"] - old_location["start_time"] ) >= self.location_time_significant: self.locations.append(old_location) loc['start_time'] = current_time() self.current_location = loc
def test_img(img,face,face_feature): if not face_feature: return None #sess = tf.Session() #init = tf.global_variables_initializer() #sess.run(init) eyes, face_grid = face_feature [x, y, w, h] = face if len(eyes) < 2: g = np.array([[0,0]]) return g start_ms = current_time() [re_x,re_y,re_w,re_h] = eyes[0] [le_x,le_y,le_w,le_h] = eyes[1] rymin = re_y rymax = re_y+re_h rxmin = re_x rxmax = re_x+re_w r_eye = img[rymin:rymax,rxmin:rxmax] r_eye = cv2.resize(r_eye,(64,64),interpolation = cv2.INTER_CUBIC) lymin = le_y lymax = le_y+le_h lxmin = le_x lxmax = le_x+le_w l_eye = img[lymin:lymax,lxmin:lxmax] l_eye = cv2.resize(l_eye,(64,64),interpolation = cv2.INTER_CUBIC) image_face = img[y:y+h,x:x+w] image_face = cv2.resize(image_face,(64,64),interpolation = cv2.INTER_CUBIC) transformed_left_eye = l_eye/255 transformed_right_eye = r_eye/255 transformed_face = image_face/255 transformed_face_grid = face_grid #print(transformed_face_grid) transformed_left_eye = np.expand_dims(transformed_left_eye,0) transformed_right_eye = np.expand_dims(transformed_right_eye,0) transformed_face = np.expand_dims(transformed_face,0) transformed_face_grid = np.expand_dims(transformed_face_grid,0) feed_dict = {a:transformed_left_eye, b:transformed_right_eye, c:transformed_face, d:transformed_face_grid} res = sess.run(e,feed_dict) print("Feeding through the network took " + str((current_time() - start_ms) * 1. / 1000) + "s") #res = res*(-255) #res = res.astype('int') return res
def status_result(self, data, args): update_tokens = data.get('update_tokens') floating_seconds = current_time() status_for_tested = data.get('status') serial_number = self.send_or_sync( { "contact_ids": [{ "id": data.get("id"), "status": status_for_tested, "duration": data.get("duration"), "update_token": update_tokens.pop(0), "message": data.get("message") }] }, floating_seconds=floating_seconds) self._update_or_result( length=len(update_tokens), floating_seconds_and_serial_number=( floating_seconds, serial_number ), # send_or_sync will use serial_number=0 and poss 1 update_tokens=update_tokens, replaces=data.get('replaces'), status=data.get('status'), message=data.get('message'), max_missing_updates=self.max_missing_updates, ) # TODO-114 maybe return how many of update_tokens used return {"status": "ok"}
def scan_status(self, data, args): since_string = data.get('since') now = current_time() req_locations = data.get('locations', []) if not self.check_bounding_box(req_locations): return { 'status': 302, 'error': "bounding boxes should be a maximum of %s sq km and specified to a resolution of %s decimal places" % (self.bb_max_size, self.bb_min_dp) } earliest_allowed = self.config.getint('DAYS_OLDEST_DATA_SENT', 21) * 24 * 60 * 60 since = max( unix_time_from_iso(since_string) if since_string else 1, earliest_allowed) prefixes = data.get('contact_prefixes') # Find any reported locations, inside the requests bounding box. # { locations: [ { min_lat...} ] } req_locations = data.get( 'locations') # None | [{min_lat, min_long, max_lat, max_long}] bounding_boxes = map( lambda l: (l['min_lat'], l['min_long'], l['max_lat'], l['max_long']), req_locations) if req_locations else None number_to_return = int(self.config.get('MAX_SCAN_COUNT', 50)) return self._scan_or_sync(prefixes, bounding_boxes, since, now, number_to_return)
def extract_image_features(img): start_ms = current_time() gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) face_detections = face_cascade.detectMultiScale(gray, 1.3, 5) # print('face detection took ' + str((current_time() - start_ms) / 1000.)) left_to_right_face_detections = sorted(face_detections, key=lambda detection: detection[0]) faces = [] face_features = [] for [x, y, w, h] in left_to_right_face_detections: face = [x, y, w, h] # start_eyes = current_time() eyes = detect_eyes(face, img, gray) # print('eye extraction ' + str((current_time() - start_eyes) / 1000.)) face_grid = get_face_grid(face, img.shape[1], img.shape[0], 25) faces.append(face) face_features.append([eyes, face_grid]) #duration_ms = current_time() - start_ms # print("Face and eye extraction took: ", str(duration_ms / 1000) + "s") return img, faces, face_features
def update(self): print('called update') # flushed = self.socket.recv(flags=0) # print('the flushed stream', flushed_stream) # keep looping infinitely until the thread is stopped while True: # if the thread indicator variable is set, stop the thread # print('updating') if self.stopped: # print('returning') return # try: # print("receiving json") sockets = dict(self.poller.poll()) if self.socket in sockets: msg = self.socket.recv() print("message", msg) # msg = self.socket.recv_json(flags=0) # data = self.socket.recv(flags=0, copy=True, track=False) # A = np.frombuffer(data, msg['dtype']) # self.frame = A.reshape(msg['shape']) # _= self.socket.recv(flags=0, copy=True, track=False) # # except KeyboardInterrupt: # # print("W: interrupt received, stopping...") # # self.worker.close() # # self.context.term() # self.frame = A.reshape(msg['shape']) # print('got socket:', self.socket.recv()) self.frame_time = current_time() self.worker.close()
def expire_data(self): """ Expire old location and id data """ expiry_time = current_time() - self.expire_locations_seconds Client._expire_from(self.locations, 'end_time', expiry_time) Client._expire_from(self.daily_ids_used, 'last_used', expiry_time) Client._expire_from(self.id_alerts, 'received_at', expiry_time)
def move_expired_data_to_deletion_list(self): until = current_time() - self.config.getint('expire_data', 45) * 24 * 60 * 60 for the_dict in [ self.contact_dict, self.spatial_dict, self.unused_update_tokens ]: the_dict.move_expired_data_to_deletion_list(0, until) return
def new_daily_id(self, id_str=None): """ The client's id is set to a new random value, and a record is kept of what we have used. """ if self.daily_id is not None: self.daily_id['last_used'] = current_time() self.daily_ids_used.append(self.daily_id) self.daily_id = { "id": id_str or "%X" % random.randrange(0, 2**128), "len": 0 }
def update(self): # keep looping infinitely until the thread is stopped while True: # if the thread indicator variable is set, stop the thread # print('updating') if self.stopped: # print('returning') return # otherwise, read the next frame from the stream (self.grabbed, self.frame) = self.stream.read() self.frame_time = current_time()
def __init__(self, socket_video_stream): print('initializing stream') self.webcam_stream = socket_video_stream print('initialized') self.img = None self.faces = None self.face_features = None self.frame_time = current_time() # initialize the variable used to indicate if the thread should # be stopped self.stopped = False
def __init__(self, src=0): # initialize the video camera stream and read the first frame # from the stream cap = cv2.VideoCapture(src) cap.set(3,1280) cap.set(4,720) self.stream = cap (self.grabbed, self.frame) = self.stream.read() self.frame_time = current_time() # initialize the variable used to indicate if the thread should # be stopped self.stopped = False
def sync(self, data, args): # Note that any replaced items will be sent as new items, so there is no need for a separate list of update_tokens. # Do this at the start of the process, we want to guarantee have all before this time (even if multi-threading) now = current_time() since_string = args.get('since') earliest_allowed = self.config.getint('DAYS_OLDEST_DATA_SENT', 21) * 24 * 60 * 60 # since_string is a list of since parameters (since named parameters can occur multiple times, data comes in as bytes, and # the decode is to turn it into a string since = max( unix_time_from_iso(since_string[0].decode()) if since_string else 1, earliest_allowed) number_to_return = int(self.config.get('MAX_SYNC_COUNT', 1000)) return self._scan_or_sync(None, None, since, now, number_to_return)
def send_or_sync(self, data, repeated_fields=None, floating_seconds=None): if not floating_seconds: floating_seconds = current_time() serial_number = 0 # first process contacts, then process geocode for contact in data.get('contact_ids', []): contact.update(repeated_fields or {}) self._insert_blob_with_optional_replacement( self.contact_dict, contact, (floating_seconds, serial_number)) # increase by two each time to deal with potential second insert serial_number += 2 for location in data.get('locations', []): location.update(repeated_fields or {}) self._insert_blob_with_optional_replacement( self.spatial_dict, location, (floating_seconds, serial_number)) # increase by two each time to deal with potential second insert serial_number += 2 return serial_number
def got_tested(self, provider_id="", test_id="", pin=""): logging.info("%s: got tested at %s testid=%s pin=%s" % (self.name, provider_id, test_id, pin)) self.local_status = STATUS_PUI provider_daily = get_provider_daily(provider_id, test_id, pin) provider_proof = get_id_proof(provider_daily) # This will update any existing entries with update_tokens from a known seed self._recalculate_status(seed=provider_proof) self.new_daily_id(provider_daily) # Saves as ued with len=0 self.new_id() # Uses the 0-th and increments # Record the NEW current_id as a pending test, when we see a notification (either way) we'll clear this self.pending_test = { "id": self.current_id, "provider_id": provider_id, "test_id": test_id, "pin": pin, "time": current_time() } self.new_daily_id() # Don't use the special daily id again self.new_id() # Uses the 0-th and increments
from gaze_detector_from_camera_stream import GazeDetectorStream import json import socket from WebcamVideoStream import WebcamVideoStream from FaceAndEyeDetectorStream import FaceAndEyeDetectorStream from gaze import test_faces from lib import current_time from gaze_detector import extract_features_and_detect_gazes vs = FaceAndEyeDetectorStream(0).start() last_read = current_time() for i=1:10 # print('Connected by', addr) while True: img, faces, face_features = vs.read() # print(img.shape, faces, face_features) if img is not None: outputs = test_faces(img, faces, face_features) if len(outputs) > 0: print('time between frames', (current_time() - last_read) * 1. / 1000) last_read = current_time() # if not data: break # do whatever you need to do with the data finally: vs.stop()
def main(): # initialize the video camera stream and read the first frame # from the stream context = zmq.Context() pull_socket = context.socket(zmq.PULL) pull_socket.bind('tcp://*:5555') push_socket = context.socket(zmq.PUSH) push_socket.bind('tcp://*:5556') video_stream = SocketVideoStream(context, pull_socket).start() face_detector_stream = FaceAndEyeDetectorWorker(video_stream).start() last_frame_time = None while True: img, faces, face_features, frame_time = face_detector_stream.read() # frontend.send_json(['hi']) if last_frame_time == frame_time: continue last_frame_time = frame_time response = {} if (faces is not None): # dumped = json.dumps(faces) faces_list = list(map(lambda face: to_int_list(face) if face is not None else [], faces)) response['faces'] = faces_list response['eyes'] = list(map(lambda face_feature: get_eyes(face_feature), face_features)) # print('faces', response['faces']) # print('jsoned', json.dumps(faces_list)) # print("faces fson:", json.dumps(response['faces'])) # # response['eyes'] = map(lambda face_feature: face_feature[0] if face_feature is not None else [], face_features) # response['face_features'] = face_features if has_valid_feature(faces, face_features): print('has valid features') outputs = gaze.test_faces(img, faces, face_features) print('outputs, detection time', outputs, current_time() - frame_time) if outputs and outputs[0] is not None: print('sending json') # response = { # 'outputs:', outputs[0].tolist(), # 'faces': faces, # 'face_features': face_features # } response['gazes'] = list(map(lambda output: output.tolist() if output is not None else [], outputs)) # else: # push_socket.send_json({ # 'faces': faces, # 'face_features': face_features # }) # print("sending response", response) # print("final json:", json.dumps(response)) push_socket.send_json(response) time.sleep(10./1000)
def _should_cache(self, floating_seconds_and_serial_number): return (current_time() - floating_seconds_and_serial_number[0] ) < self.disk_cache_retention_time
from FaceAndEyeDetectorStream import FaceAndEyeDetectorStream from gaze import test_faces from lib import current_time, smooth_outputs from gaze_detector import extract_features_and_detect_gazes import warnings warnings.filterwarnings("ignore") vs = FaceAndEyeDetectorStream(0).start() last_read = current_time() set_to_gpu = False previous_outputs = [] previous_frame_time = None try: while True: img, faces, face_features, frame_time = vs.read() # print(img.shape, faces, face_features) if img is not None: new_outputs = test_faces(img, faces, face_features) if len(new_outputs) > 0: outputs = smooth_outputs(new_outputs, frame_time, previous_outputs, previous_frame_time) print('original outputs', new_outputs) print('smoothed outputs', outputs) previous_outputs = outputs previous_frame_time = frame_time
def status_update(self, data, args): logger.info('in status_update') self._update_or_result( floating_seconds_and_serial_number=(current_time(), 0), **data) return {"status": "ok"}
def poll(self): """ Perform a regular poll of the server with /status/scan, and process any results. """ json_data = self.server.scan_status_json( contact_prefixes=self._prefixes(), locations=[self._box()] if len(self.locations) else [], since=self.since) logging.info("%s: poll result: %s" % (self.name, str(json_data))) # Record when data is updated till, for our next request self.since = json_data.get('until') # Record any ids in the poll that match one we have used (id = {id, last_used}) # Note that this can include a test result which might be STATUS_HEALTHY matched_ids = [ i for i in json_data['contact_ids'] if i.get('id') in self.map_ids_used() ] for id_obj in matched_ids: id_obj['received_at'] = current_time() # Scan for a test result and flag the id we will record (via its 'test' field) so that it can effect score calculations if self.pending_test: if id_obj['id'] == self.pending_test['id']: id_obj[ 'test'] = self.pending_test # Save the test (includes test_id and pin) # Time of test -> old daily_ids (t-1day) -> ids used on those -> remove id_alerts; location_alerts dated < t-1day ignore_alerts_before = self.pending_test[ "time"] - self.on_test_ignore_before self.pending_test = None # Clear pending test # Clear out older alerts for alert_list in self.id_alerts, self.location_alerts: self.id_alerts = [ alert_obj for alert_obj in alert_list if alert_obj['received_at'] < ignore_alerts_before ] self.local_status = STATUS_HEALTHY # Note this is correct even if the test is INFECTED, as its the infected test that counts, and there is no LOCAL status event any more self.id_alerts.extend( matched_ids ) # Add the new ones after we have cleared out alerts no longer valid # Filter incoming location updates for those close to where we have been, # but exclude any of our own (based on matching update_token existing_location_update_tokens = [ loc.get('update_token') for loc in self.locations ] matching_locations = [ loc for loc in json_data.get('locations', []) if self._location_match(loc) and not loc.get('update_token') in existing_location_update_tokens ] for loc in matching_locations: loc['received_at'] = current_time() self.location_alerts.extend(matching_locations) # Look for any updated data points # Find the replaces tokens for both ids and locations - these are the locations this data point replaces # Note that by checking all id_alerts we also handle any received out of order (replace received before original) id_replaces = [ i.get('replaces') for i in self.id_alerts if i.get('replaces') ] location_replaces = [ loc.get('replaces') for loc in self.location_alerts if loc.get('replaces') ] # Find update_tokens that have been replaced id_update_tokens = [get_update_token(rt) for rt in id_replaces] location_update_tokens = [ get_update_token(rt) for rt in location_replaces ] # Mark any ids or locations that have been replaced for i in self.id_alerts: if i.get('update_token') in id_update_tokens: i['replaced'] = True for loc in self.location_alerts: if loc.get('update_token') in location_update_tokens: loc['replaced'] = True # Recalculate our own status based on the current set of location and id alerts and our local_status # Note that if it has changed it may trigger a notify_status which can cause a /status/send or /status/update self._recalculate_status()
output_string += str(output[0]) + ',' + str(output[1]) if (i < len(outputs) - 1): output_string += '_' # print("output string", output_string) return output_string HOST = '' # Symbolic name meaning all available interfaces PORT = 4001 # Arbitrary non-privileged port vs = FaceAndEyeDetectorStream(0).start() s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) s.listen(1) conn, addr = s.accept() last_read = current_time() set_to_gpu = False previous_outputs = [] previous_frame_time = None try: # print('Connected by', addr) while True: img, faces, face_features, frame_time = vs.read() # print(img.shape, faces, face_features) if img is not None: new_outputs = test_faces(img, faces, face_features) if len(new_outputs) > 0: