From 1edcf73a73d2007afc61289245763f5cf0c29e10 Mon Sep 17 00:00:00 2001
From: Joseph Redmon <pjreddie@gmail.com>
Date: Thu, 04 Dec 2014 07:20:29 +0000
Subject: [PATCH] Detection good, split up col images
---
src/utils.c | 108 +++++++++++++++++++++++++++++++++++++++--------------
1 files changed, 79 insertions(+), 29 deletions(-)
diff --git a/src/utils.c b/src/utils.c
index 67a9ba1..bba6218 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -1,8 +1,50 @@
-#include "utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
+#include <float.h>
+
+#include "utils.h"
+
+char *find_replace(char *str, char *orig, char *rep)
+{
+ static char buffer[4096];
+ char *p;
+
+ if(!(p = strstr(str, orig))) // Is 'orig' even in 'str'?
+ return str;
+
+ strncpy(buffer, str, p-str); // Copy characters from 'str' start to 'orig' st$
+ buffer[p-str] = '\0';
+
+ sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));
+
+ return buffer;
+}
+
+float sec(clock_t clocks)
+{
+ return (float)clocks/CLOCKS_PER_SEC;
+}
+
+void top_k(float *a, int n, int k, int *index)
+{
+ int i,j;
+ float thresh = FLT_MAX;
+ for(i = 0; i < k; ++i){
+ float max = -FLT_MAX;
+ int max_i = -1;
+ for(j = 0; j < n; ++j){
+ float val = a[j];
+ if(val > max && val < thresh){
+ max = val;
+ max_i = j;
+ }
+ }
+ index[i] = max_i;
+ thresh = max;
+ }
+}
void error(char *s)
{
@@ -66,7 +108,7 @@
char *fgetl(FILE *fp)
{
if(feof(fp)) return 0;
- int size = 512;
+ unsigned long size = 512;
char *line = malloc(size*sizeof(char));
if(!fgets(line, size, fp)){
free(line);
@@ -74,11 +116,14 @@
}
int curr = strlen(line);
-
+
while(line[curr-1]!='\n'){
size *= 2;
line = realloc(line, size*sizeof(char));
- if(!line) malloc_error();
+ if(!line) {
+ printf("%ld\n", size);
+ malloc_error();
+ }
fgets(&line[curr], size-curr, fp);
curr = strlen(line);
}
@@ -113,42 +158,47 @@
int count_fields(char *line)
{
- int count = 0;
- int done = 0;
+ int count = 0;
+ int done = 0;
char *c;
- for(c = line; !done; ++c){
- done = (*c == '\0');
- if(*c == ',' || done) ++count;
- }
- return count;
+ for(c = line; !done; ++c){
+ done = (*c == '\0');
+ if(*c == ',' || done) ++count;
+ }
+ return count;
}
float *parse_fields(char *line, int n)
{
- float *field = calloc(n, sizeof(float));
- char *c, *p, *end;
- int count = 0;
- int done = 0;
- for(c = line, p = line; !done; ++c){
- done = (*c == '\0');
- if(*c == ',' || done){
- *c = '\0';
- field[count] = strtod(p, &end);
- if(p == c) field[count] = nan("");
- if(end != c && (end != c-1 || *end != '\r')) field[count] = nan(""); //DOS file formats!
- p = c+1;
- ++count;
- }
- }
- return field;
+ float *field = calloc(n, sizeof(float));
+ char *c, *p, *end;
+ int count = 0;
+ int done = 0;
+ for(c = line, p = line; !done; ++c){
+ done = (*c == '\0');
+ if(*c == ',' || done){
+ *c = '\0';
+ field[count] = strtod(p, &end);
+ if(p == c) field[count] = nan("");
+ if(end != c && (end != c-1 || *end != '\r')) field[count] = nan(""); //DOS file formats!
+ p = c+1;
+ ++count;
+ }
+ }
+ return field;
}
-float mean_array(float *a, int n)
+float sum_array(float *a, int n)
{
int i;
float sum = 0;
for(i = 0; i < n; ++i) sum += a[i];
- return sum/n;
+ return sum;
+}
+
+float mean_array(float *a, int n)
+{
+ return sum_array(a,n)/n;
}
float variance_array(float *a, int n)
--
Gitblit v1.10.0