From c31c891959b0d1b238ee3bbb4094b702622bebe8 Mon Sep 17 00:00:00 2001 From: John Gebbie Date: Wed, 3 May 2023 09:02:08 +0100 Subject: [PATCH 1/2] tidy: not x is None -> x is not None --- dshowcapture.py | 6 +++--- facetracker.py | 34 +++++++++++++++++----------------- tracker.py | 2 +- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/dshowcapture.py b/dshowcapture.py index 46def39..515125c 100644 --- a/dshowcapture.py +++ b/dshowcapture.py @@ -77,7 +77,7 @@ class DShowCapture(): bm_lib.stop_capture_single.argtypes = [] bm_lib.get_json_length.argtypes = [] bm_lib.get_json.argtypes = [c_char_p, c_int] - if not bm_options is None: + if bm_options is not None: bm_lib.set_options.argtypes = [c_char_p] bm_lib.set_options(bm_options.encode()) self.lib = lib @@ -93,7 +93,7 @@ class DShowCapture(): self.type = None def __del__(self): - if not self.buffer is None: + if self.buffer is not None: del self.buffer del self.name_buffer self.destroy_capture() @@ -329,7 +329,7 @@ if __name__ == "__main__": while True: img = cap.get_frame(1000) - if not img is None: + if img is not None: cv2.imshow("DShowCapture", img) if cv2.waitKey(1) & 0xFF == ord('q'): sys.exit(0) diff --git a/facetracker.py b/facetracker.py index 4f05c07..c629835 100644 --- a/facetracker.py +++ b/facetracker.py @@ -60,12 +60,12 @@ class OutputLog(object): self.fh = fh self.output = output def write(self, buf): - if not self.fh is None: + if self.fh is not None: self.fh.write(buf) self.output.write(buf) self.flush() def flush(self): - if not self.fh is None: + if self.fh is not None: self.fh.flush() self.output.flush() output_logfile = None @@ -78,19 +78,19 @@ if os.name == 'nt': import dshowcapture if args.blackmagic == 1: dshowcapture.set_bm_enabled(True) - if not args.blackmagic_options is None: + if args.blackmagic_options is not None: dshowcapture.set_options(args.blackmagic_options) - if not args.priority is None: + if args.priority is not None: import psutil classes = [psutil.IDLE_PRIORITY_CLASS, psutil.BELOW_NORMAL_PRIORITY_CLASS, psutil.NORMAL_PRIORITY_CLASS, psutil.ABOVE_NORMAL_PRIORITY_CLASS, psutil.HIGH_PRIORITY_CLASS, psutil.REALTIME_PRIORITY_CLASS] p = psutil.Process(os.getpid()) p.nice(classes[args.priority]) -if os.name == 'nt' and (args.list_cameras > 0 or not args.list_dcaps is None): +if os.name == 'nt' and (args.list_cameras > 0 or args.list_dcaps is not None): cap = dshowcapture.DShowCapture() info = cap.get_info() unit = 10000000.; - if not args.list_dcaps is None: + if args.list_dcaps is not None: formats = {0: "Any", 1: "Unknown", 100: "ARGB", 101: "XRGB", 200: "I420", 201: "NV12", 202: "YV12", 203: "Y800", 300: "YVYU", 301: "YUY2", 302: "UYVY", 303: "HDYC (Unsupported)", 400: "MJPEG", 401: "H264" } for cam in info: if args.list_dcaps == -1: @@ -248,7 +248,7 @@ try: height, width, channels = frame.shape sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) tracker = Tracker(width, height, threshold=args.threshold, max_threads=args.max_threads, max_faces=args.faces, discard_after=args.discard_after, scan_every=args.scan_every, silent=False if args.silent == 0 else True, model_type=args.model, model_dir=args.model_dir, no_gaze=False if args.gaze_tracking != 0 and args.model != -1 else True, detection_threshold=args.detection_threshold, use_retinaface=args.scan_retinaface, max_feature_updates=args.max_feature_updates, static_model=True if args.no_3d_adapt == 1 else False, try_hard=args.try_hard == 1) - if not args.video_out is None: + if args.video_out is not None: out = cv2.VideoWriter(args.video_out, cv2.VideoWriter_fourcc('F','F','V','1'), args.video_fps, (width * args.video_scale, height * args.video_scale)) try: @@ -291,7 +291,7 @@ try: packet.extend(bytearray(struct.pack("f", f.translation[0]))) packet.extend(bytearray(struct.pack("f", f.translation[1]))) packet.extend(bytearray(struct.pack("f", f.translation[2]))) - if not log is None: + if log is not None: log.write(f"{frame_count},{now},{width},{height},{fps},{face_num},{f.id},{f.eye_blink[0]},{f.eye_blink[1]},{f.conf},{f.success},{f.pnp_error},{f.quaternion[0]},{f.quaternion[1]},{f.quaternion[2]},{f.quaternion[3]},{f.euler[0]},{f.euler[1]},{f.euler[2]},{f.rotation[0]},{f.rotation[1]},{f.rotation[2]},{f.translation[0]},{f.translation[1]},{f.translation[2]}") for (x,y,c) in f.lms: packet.extend(bytearray(struct.pack("f", c))) @@ -302,7 +302,7 @@ try: for pt_num, (x,y,c) in enumerate(f.lms): packet.extend(bytearray(struct.pack("f", y))) packet.extend(bytearray(struct.pack("f", x))) - if not log is None: + if log is not None: log.write(f",{y},{x},{c}") if pt_num == 66 and (f.eye_blink[0] < 0.30 or c < 0.20): continue @@ -310,7 +310,7 @@ try: continue x = int(x + 0.5) y = int(y + 0.5) - if args.visualize != 0 or not out is None: + if args.visualize != 0 or out is not None: if args.visualize > 3: frame = cv2.putText(frame, str(pt_num), (int(y), int(x)), cv2.FONT_HERSHEY_SIMPLEX, 0.25, (255,255,0)) color = (0, 255, 0) @@ -327,7 +327,7 @@ try: x -= 1 if not (x < 0 or y < 0 or x >= height or y >= width): frame[int(x), int(y)] = color - if args.pnp_points != 0 and (args.visualize != 0 or not out is None) and f.rotation is not None: + if args.pnp_points != 0 and (args.visualize != 0 or out is not None) and f.rotation is not None: if args.pnp_points > 1: projected = cv2.projectPoints(f.face_3d[0:66], f.rotation, f.translation, tracker.camera, tracker.dist_coeffs) else: @@ -350,7 +350,7 @@ try: packet.extend(bytearray(struct.pack("f", x))) packet.extend(bytearray(struct.pack("f", -y))) packet.extend(bytearray(struct.pack("f", -z))) - if not log is None: + if log is not None: log.write(f",{x},{-y},{-z}") if f.current_features is None: f.current_features = {} @@ -358,16 +358,16 @@ try: if not feature in f.current_features: f.current_features[feature] = 0 packet.extend(bytearray(struct.pack("f", f.current_features[feature]))) - if not log is None: + if log is not None: log.write(f",{f.current_features[feature]}") - if not log is None: + if log is not None: log.write("\r\n") log.flush() if detected and len(faces) < 40: sock.sendto(packet, (target_ip, target_port)) - if not out is None: + if out is not None: video_frame = frame if args.video_scale != 1: video_frame = cv2.resize(frame, (width * args.video_scale, height * args.video_scale), interpolation=cv2.INTER_NEAREST) @@ -378,7 +378,7 @@ try: if args.visualize != 0: cv2.imshow('OpenSeeFace Visualization', frame) if cv2.waitKey(1) & 0xFF == ord('q'): - if args.dump_points != "" and not faces is None and len(faces) > 0: + if args.dump_points != "" and faces is not None and len(faces) > 0: np.set_printoptions(threshold=sys.maxsize, precision=15) pairs = [ (0, 16), @@ -454,7 +454,7 @@ except KeyboardInterrupt: print("Quitting") input_reader.close() -if not out is None: +if out is not None: out.release() cv2.destroyAllWindows() diff --git a/tracker.py b/tracker.py index d3a1cc4..0854775 100644 --- a/tracker.py +++ b/tracker.py @@ -767,7 +767,7 @@ class Tracker(): image_pts = np.array(lms)[face_info.contour_pts, 0:2] success = False - if not face_info.rotation is None: + if face_info.rotation is not None: success, face_info.rotation, face_info.translation = cv2.solvePnP(face_info.contour, image_pts, self.camera, self.dist_coeffs, useExtrinsicGuess=True, rvec=np.transpose(face_info.rotation), tvec=np.transpose(face_info.translation), flags=cv2.SOLVEPNP_ITERATIVE) else: rvec = np.array([0, 0, 0], np.float32) From 96a9a2549af23363e08bdd3e8c6ed3219dbe5dff Mon Sep 17 00:00:00 2001 From: John Gebbie Date: Wed, 3 May 2023 09:39:36 +0100 Subject: [PATCH 2/2] tidy: use cv2.circle --- facetracker.py | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/facetracker.py b/facetracker.py index c629835..a30628e 100644 --- a/facetracker.py +++ b/facetracker.py @@ -317,16 +317,7 @@ try: if pt_num >= 66: color = (255, 255, 0) if not (x < 0 or y < 0 or x >= height or y >= width): - frame[int(x), int(y)] = color - x += 1 - if not (x < 0 or y < 0 or x >= height or y >= width): - frame[int(x), int(y)] = color - y += 1 - if not (x < 0 or y < 0 or x >= height or y >= width): - frame[int(x), int(y)] = color - x -= 1 - if not (x < 0 or y < 0 or x >= height or y >= width): - frame[int(x), int(y)] = color + cv2.circle(frame, (y, x), 1, color, -1) if args.pnp_points != 0 and (args.visualize != 0 or out is not None) and f.rotation is not None: if args.pnp_points > 1: projected = cv2.projectPoints(f.face_3d[0:66], f.rotation, f.translation, tracker.camera, tracker.dist_coeffs)