Skip to content

Add object detection, tracking, mobile notifications, and search to any security camera.

different req for frames

roryclearc04f5875954e
diff --git a/clearcam/FileServer.m b/clearcam/FileServer.m
index 42f7865..4f1f9b6 100644
--- a/clearcam/FileServer.m
+++ b/clearcam/FileServer.m
@@ -135,6 +135,97 @@
return processedSegments;
}
+- (NSArray *)fetchFramesWithURLsFromCoreDataForDateParam:(NSString *)dateParam
+ start:(NSInteger)start
+ context:(NSManagedObjectContext *)context {
+ if (!context) {
+ NSLog(@"Context is nil, skipping fetch.");
+ return @[];
+ }
+
+ __block NSArray *copiedSegments = @[];
+
+ [context performBlockAndWait:^{
+ NSError *error = nil;
+
+ // Fetch the DayEntity for the given date
+ NSFetchRequest *dayFetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"DayEntity"];
+ dayFetchRequest.predicate = [NSPredicate predicateWithFormat:@"date == %@", dateParam];
+
+ NSArray *fetchedDays = [context executeFetchRequest:dayFetchRequest error:&error];
+
+ if (error) {
+ NSLog(@"Failed to fetch DayEntity: %@", error.localizedDescription);
+ return;
+ }
+
+ if (fetchedDays.count == 0) {
+ NSLog(@"No DayEntity found for date %@", dateParam);
+ return;
+ }
+
+ NSManagedObject *dayEntity = fetchedDays.firstObject;
+ NSOrderedSet *segments = [dayEntity valueForKey:@"segments"];
+
+ // Slice the segments based on the 'start' parameter to avoid fetching everything
+ if (start >= segments.count) {
+ NSLog(@"Start index out of range (%ld/%lu)", (long)start, (unsigned long)segments.count);
+ return;
+ }
+
+ copiedSegments = [[segments array] subarrayWithRange:NSMakeRange(start, segments.count - start)];
+ }];
+
+ // Process outside of performBlockAndWait to avoid blocking Core Data
+ NSMutableArray *framesWithURLs = [NSMutableArray array];
+
+ for (NSManagedObject *segment in copiedSegments) {
+ NSString *url = [segment valueForKey:@"url"];
+ NSArray *frames = [[segment valueForKey:@"frames"] array]; // Copy frames
+
+ for (NSManagedObject *frame in frames) {
+ double frameTimeStamp = [[frame valueForKey:@"frame_timeStamp"] doubleValue];
+ double aspectRatio = [[frame valueForKey:@"aspect_ratio"] doubleValue];
+ int res = [[frame valueForKey:@"res"] intValue];
+
+ NSMutableArray *squareDicts = [NSMutableArray array];
+ NSArray *squares = [[frame valueForKey:@"squares"] array]; // Copy squares
+
+ for (NSManagedObject *square in squares) {
+ double originX = [[square valueForKey:@"originX"] doubleValue];
+ double originY = [[square valueForKey:@"originY"] doubleValue];
+ double bottomRightX = [[square valueForKey:@"bottomRightX"] doubleValue];
+ double bottomRightY = [[square valueForKey:@"bottomRightY"] doubleValue];
+ int classIndex = [[square valueForKey:@"classIndex"] intValue];
+
+ NSDictionary *squareDict = @{
+ @"originX": @(originX),
+ @"originY": @(originY),
+ @"bottomRightX": @(bottomRightX),
+ @"bottomRightY": @(bottomRightY),
+ @"classIndex": @(classIndex)
+ };
+
+ [squareDicts addObject:squareDict];
+ }
+
+ NSDictionary *frameDict = @{
+ @"url": url, // Include the segment's URL
+ @"frame_timeStamp": @(frameTimeStamp),
+ @"aspect_ratio": @(aspectRatio),
+ @"res": @(res),
+ @"squares": squareDicts
+ };
+
+ [framesWithURLs addObject:frameDict];
+ }
+ }
+
+ NSLog(@"Fetched and processed %lu frames with URLs (start=%ld) for date %@",
+ (unsigned long)framesWithURLs.count, (long)start, dateParam);
+
+ return framesWithURLs;
+}
- (void)startHTTPServerWithBasePath:(NSString *)basePath {
@try {
@@ -333,6 +424,45 @@
send(clientSocket, slicedJsonData.bytes, slicedJsonData.length, 0);
}
+ if ([filePath hasPrefix:@"get-frames"]) {
+ NSString *urlParam = nil;
+ NSRange queryRange = [filePath rangeOfString:@"?"];
+ if (queryRange.location != NSNotFound) {
+ NSString *queryString = [filePath substringFromIndex:queryRange.location + 1];
+ NSArray *queryItems = [queryString componentsSeparatedByString:@"&"];
+ for (NSString *item in queryItems) {
+ NSArray *keyValue = [item componentsSeparatedByString:@"="];
+ if (keyValue.count == 2 && [keyValue[0] isEqualToString:@"url"]) {
+ urlParam = keyValue[1];
+ break;
+ }
+ }
+ }
+
+ if (!urlParam) {
+ NSString *httpHeader = @"HTTP/1.1 400 Bad Request\r\nContent-Type: application/json\r\n\r\n";
+ NSString *errorMessage = @"{\"error\": \"Missing or invalid url parameter\"}";
+ send(clientSocket, [httpHeader UTF8String], httpHeader.length, 0);
+ send(clientSocket, [errorMessage UTF8String], errorMessage.length, 0);
+ return;
+ }
+
+ NSArray *framesForURL = [self fetchFramesForURL:urlParam context:self.context];
+
+ if (framesForURL.count == 0) {
+ NSString *httpHeader = @"HTTP/1.1 404 Not Found\r\nContent-Type: application/json\r\n\r\n";
+ NSString *errorMessage = @"{\"error\": \"No frames found for the given URL\"}";
+ send(clientSocket, [httpHeader UTF8String], httpHeader.length, 0);
+ send(clientSocket, [errorMessage UTF8String], errorMessage.length, 0);
+ return;
+ }
+
+ NSData *jsonData = [NSJSONSerialization dataWithJSONObject:framesForURL options:0 error:nil];
+ NSString *httpHeader = [NSString stringWithFormat:@"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: %lu\r\n\r\n", (unsigned long)jsonData.length];
+ send(clientSocket, [httpHeader UTF8String], httpHeader.length, 0);
+ send(clientSocket, jsonData.bytes, jsonData.length, 0);
+ }
+
if ([filePath hasPrefix:@"get-events"]) {
NSArray *eventDataArray = [self fetchEventDataFromCoreData:self.context];
@@ -527,6 +657,84 @@
return YES;
}
+- (NSArray *)fetchFramesForURL:(NSString *)url context:(NSManagedObjectContext *)context {
+ if (!context) {
+ NSLog(@"Context is nil, skipping fetch.");
+ return @[];
+ }
+
+ __block NSArray *copiedSegments = @[];
+
+ [context performBlockAndWait:^{
+ NSError *error = nil;
+
+ // Fetch the SegmentEntity that matches the given URL
+ NSFetchRequest *segmentFetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"SegmentEntity"];
+ segmentFetchRequest.predicate = [NSPredicate predicateWithFormat:@"url == %@", url];
+
+ copiedSegments = [context executeFetchRequest:segmentFetchRequest error:&error];
+
+ if (error) {
+ NSLog(@"Failed to fetch segments for URL %@: %@", url, error.localizedDescription);
+ return;
+ }
+
+ if (copiedSegments.count == 0) {
+ NSLog(@"No segments found for URL %@", url);
+ return;
+ }
+ }];
+
+ // Process frames outside of performBlockAndWait to avoid blocking Core Data
+ NSMutableArray *framesForURL = [NSMutableArray array];
+
+ for (NSManagedObject *segment in copiedSegments) {
+ NSArray *frames = [[segment valueForKey:@"frames"] array]; // Copy frames
+
+ for (NSManagedObject *frame in frames) {
+ double frameTimeStamp = [[frame valueForKey:@"frame_timeStamp"] doubleValue];
+ double aspectRatio = [[frame valueForKey:@"aspect_ratio"] doubleValue];
+ int res = [[frame valueForKey:@"res"] intValue];
+
+ NSMutableArray *squareDicts = [NSMutableArray array];
+ NSArray *squares = [[frame valueForKey:@"squares"] array]; // Copy squares
+
+ for (NSManagedObject *square in squares) {
+ double originX = [[square valueForKey:@"originX"] doubleValue];
+ double originY = [[square valueForKey:@"originY"] doubleValue];
+ double bottomRightX = [[square valueForKey:@"bottomRightX"] doubleValue];
+ double bottomRightY = [[square valueForKey:@"bottomRightY"] doubleValue];
+ int classIndex = [[square valueForKey:@"classIndex"] intValue];
+
+ NSDictionary *squareDict = @{
+ @"originX": @(originX),
+ @"originY": @(originY),
+ @"bottomRightX": @(bottomRightX),
+ @"bottomRightY": @(bottomRightY),
+ @"classIndex": @(classIndex)
+ };
+
+ [squareDicts addObject:squareDict];
+ }
+
+ NSDictionary *frameDict = @{
+ @"url": url, // Include the segment's URL
+ @"frame_timeStamp": @(frameTimeStamp),
+ @"aspect_ratio": @(aspectRatio),
+ @"res": @(res),
+ @"squares": squareDicts
+ };
+
+ [framesForURL addObject:frameDict];
+ }
+ }
+
+ NSLog(@"Fetched and processed %lu frames for URL %@", (unsigned long)framesForURL.count, url);
+
+ return framesForURL;
+}
+
+
- (NSArray *)fetchEventDataFromCoreData:(NSManagedObjectContext *)context {
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"EventEntity"];
NSError *error = nil;
diff --git a/clearcam/player.html b/clearcam/player.html
index 380fce0..5c35076 100644
--- a/clearcam/player.html
+++ b/clearcam/player.html
@@ -534,57 +534,77 @@
}, retryTimeout);
}
- function updateTimeline() {
- const startTimestamp = segments[0].timeStamp;
- let adjustedStartTimestamp;
+ async function updateTimeline() {
+ const startTimestamp = segments[0].timeStamp;
+ let adjustedStartTimestamp;
- if (startTimestamp < 0) {
- adjustedStartTimestamp = (24 * 3600) + startTimestamp; //todo hack?
- } else {
- adjustedStartTimestamp = startTimestamp;
- }
-
- const currentTime = adjustedStartTimestamp + videoPlayers[video_index].currentTime + segments[currentSegment].cumulativeStart;
-
- let frames = segments[currentSegment].frames;
- let show_new = false;
- if(frames){
- while((shape_idx+1 < frames.length) && (currentTime > frames[shape_idx+1].frame_timeStamp)) {
- shape_idx += 1;
- show_new = true;
- }
-
- if(show_new == true){
- let n_boxes = document.querySelectorAll(".bounding-box").length;
- let aspect_ratio = frames[shape_idx].aspect_ratio;
- let res = frames[shape_idx].res;
- //drawBoundingBox(0.0,0.0,0.5,0.5,"red","rory");
- for(let i = 0; i < frames[shape_idx].squares.length; i++){
- if(portrait){ //todo, can just send the correct aspect ratio instead?
- let originX = (frames[shape_idx].squares[i].originX / (res / aspect_ratio));
- let originY = (frames[shape_idx].squares[i].originY / res);
- let length = ((frames[shape_idx].squares[i].bottomRightX - frames[shape_idx].squares[i].originX) / (res / aspect_ratio));
- let height = ((frames[shape_idx].squares[i].bottomRightY - frames[shape_idx].squares[i].originY) / res);
- drawBoundingBox(originX, originY, length, height,yolo_classes[frames[shape_idx].squares[i].classIndex][1],yolo_classes[frames[shape_idx].squares[i].classIndex][0]);
- } else {
- let originX = (frames[shape_idx].squares[i].originX / res);
- let originY = (frames[shape_idx].squares[i].originY / (res / aspect_ratio));
- let length = ((frames[shape_idx].squares[i].bottomRightX - frames[shape_idx].squares[i].originX) / res);
- let height = ((frames[shape_idx].squares[i].bottomRightY - frames[shape_idx].squares[i].originY) / (res / aspect_ratio));
- drawBoundingBox(originX, originY, length, height,yolo_classes[frames[shape_idx].squares[i].classIndex][1],yolo_classes[frames[shape_idx].squares[i].classIndex][0]);
- }
- }
- let boundingBoxes = document.querySelectorAll(".bounding-box");
- for (let i = 0; i < n_boxes; i++) {
- boundingBoxes[i].remove();
- }
+ if (startTimestamp < 0) {
+ adjustedStartTimestamp = (24 * 3600) + startTimestamp; // Possible hack?
+ } else {
+ adjustedStartTimestamp = startTimestamp;
+ }
+
+ const currentTime = adjustedStartTimestamp + videoPlayers[video_index].currentTime + segments[currentSegment].cumulativeStart;
+
+ // Check if frames exist; otherwise, fetch them dynamically (if not already fetching)
+ if (!segments[currentSegment].frames && !segments[currentSegment].fetchingFrames) {
+ segments[currentSegment].fetchingFrames = true; // Set fetching flag
+ try {
+ const response = await fetch(`/get-frames?url=${encodeURIComponent(segments[currentSegment].url)}`);
+ if (!response.ok) throw new Error(`Failed to fetch frames for ${segments[currentSegment].url}`);
+
+ const frames = await response.json();
+ segments[currentSegment].frames = frames; // Store fetched frames to avoid re-fetching
+ } catch (error) {
+ console.error("Error fetching frames:", error);
+ }
+ segments[currentSegment].fetchingFrames = false; // Reset fetching flag
+ }
+
+ let frames = segments[currentSegment].frames;
+ let show_new = false;
+
+ if (frames) {
+ while ((shape_idx + 1 < frames.length) && (currentTime > frames[shape_idx + 1].frame_timeStamp)) {
+ shape_idx += 1;
+ show_new = true;
+ }
+
+ if (show_new) {
+ let n_boxes = document.querySelectorAll(".bounding-box").length;
+ let aspect_ratio = frames[shape_idx].aspect_ratio;
+ let res = frames[shape_idx].res;
+
+ for (let i = 0; i < frames[shape_idx].squares.length; i++) {
+ if (portrait) {
+ let originX = frames[shape_idx].squares[i].originX / (res / aspect_ratio);
+ let originY = frames[shape_idx].squares[i].originY / res;
+ let length = (frames[shape_idx].squares[i].bottomRightX - frames[shape_idx].squares[i].originX) / (res / aspect_ratio);
+ let height = (frames[shape_idx].squares[i].bottomRightY - frames[shape_idx].squares[i].originY) / res;
+ drawBoundingBox(originX, originY, length, height, yolo_classes[frames[shape_idx].squares[i].classIndex][1], yolo_classes[frames[shape_idx].squares[i].classIndex][0]);
+ } else {
+ let originX = frames[shape_idx].squares[i].originX / res;
+ let originY = frames[shape_idx].squares[i].originY / (res / aspect_ratio);
+ let length = (frames[shape_idx].squares[i].bottomRightX - frames[shape_idx].squares[i].originX) / res;
+ let height = (frames[shape_idx].squares[i].bottomRightY - frames[shape_idx].squares[i].originY) / (res / aspect_ratio);
+ drawBoundingBox(originX, originY, length, height, yolo_classes[frames[shape_idx].squares[i].classIndex][1], yolo_classes[frames[shape_idx].squares[i].classIndex][0]);
}
}
-
- progressBar.style.width = `${(currentTime - adjustedStartTimestamp) / totalDuration * 100}%`;
- timeDisplay.innerText = `${formatTime(currentTime)} / ${formatTime(adjustedStartTimestamp + totalDuration)}`;
- updateGaps();
+
+ // Remove old bounding boxes
+ let boundingBoxes = document.querySelectorAll(".bounding-box");
+ for (let i = 0; i < n_boxes; i++) {
+ boundingBoxes[i].remove();
+ }
}
+ }
+
+ progressBar.style.width = `${(currentTime - adjustedStartTimestamp) / totalDuration * 100}%`;
+ timeDisplay.innerText = `${formatTime(currentTime)} / ${formatTime(adjustedStartTimestamp + totalDuration)}`;
+ updateGaps();
+ }
+
+
function updateGaps() {