diff --git a/pa_job_manager.py b/pa_job_manager.py index c4eb5d0..dcd1349 100644 --- a/pa_job_manager.py +++ b/pa_job_manager.py @@ -1339,6 +1339,61 @@ def InitialValidationChecks(): FinishJob(job,"Finished Initial Validation Checks") return +#### CAM: New FACES/AI code + +def AddFaceToFile( face_data, file_eid ): + face = Face( face=face_data ) + session.add(face) + session.commit() + ffl = FaceFileLink( face_id=face.id, file_eid=file_eid ) + session.add(ffl) + session.commit() + return face + +def DelFacesForFile( eid ): + session.execute( f"delete from face where id in (select face_id from face_file_link where file_eid = {eid})" ) + session.commit() + return + +def MatchRefimgToFace( refimg_id, face_id ): + rfl = FaceRefimgLink( refimg_id = refimg_id, face_id = face_id ) + session.add(rfl) + session.commit() + return + +def UnmatchedFacesForFile( eid ): + rows = session.execute( f"select f.id, ffl.file_eid, frl.refimg_id from face f left join face_refimg_link frl on f.id = frl.face_id join face_file_link ffl on f.id = ffl.face_id where ffl.file_eid = {eid} and frl.refimg_id is null" ) + return rows + +### CAM: something like this -- HAVE NOT TRIED THIS IT WILL FAIL### +def ScanFileForPerson( eid, person_id, force=False ): + file_h = session.query(File).get( eid ) + # if we are forcing this, delete any old faces (this will also delete linked tables), and reset faces_created_on to None + if force: + DelFacesForFile( eid ) + file_h.faces_create_on = None + + # optimise: dont rescan if we already have faces (we are just going to try + # to match (maybe?) a refimg + if not file_h.faces_created_on: + # CAM: TODO: add Face Rec code to get unknown encodings + for face in unknown_encodings: + new_face = Face( face_data = face ) + session.add(new_face) + session.commit() + AddFaceToFile( new_face.id, eid ) + now=datetime.now(pytz.utc) + file_h.face_created_on = now + + ## now look for person + refimgs = session.query(Refimg).join(PersonRefimgLink).filter(PersonRefimgLink.person_id==person_id).all() + uf = UnmatchedFacesForFile( eid ) + for face in uf: + for r in refimgs: + # CAM: TODO: add Face rec code to see if there is match + if match: + MatchRefimgToFace( r.id, face.id ) + return if __name__ == "__main__": print("INFO: PA job manager starting - listening on {}:{}".format( PA_JOB_MANAGER_HOST, PA_JOB_MANAGER_PORT) )