private static void extract_voxel(string lfile, string rfile, string prefix) { int w = 1920; int h = 1080; int shift = 0; int count = 0; VideoCapture lcap = new VideoCapture(lfile); VideoCapture rcap = new VideoCapture(rfile); while (true) { Image l = LoadArgs.get_image_from_stream(lcap); Image r = LoadArgs.get_image_from_stream(rcap); if (l.W == 0 || r.W == 0) { break; } if (count % 100 == 0) { shift = LoadArgs.best_3d_shift_r(l, r, -l.H / 100, l.H / 100); Console.Write($"{shift}\n"); } Image ls = LoadArgs.crop_image(l, (l.W - w) / 2, (l.H - h) / 2, w, h); Image rs = LoadArgs.crop_image(r, 105 + (r.W - w) / 2, (r.H - h) / 2 + shift, w, h); string buff = $"{prefix}_{count:05}_l"; LoadArgs.save_image(ls, buff); buff = $"{prefix}_{count:05}_r"; LoadArgs.save_image(rs, buff); ++count; } }
private static void fetch_in_thread() { inputImage = LoadArgs.get_image_from_stream(vidCap); if (inputImage.Data.Length == 0) { Utils.Error("Stream closed."); } inS = LoadArgs.resize_image(inputImage, net.W, net.H); }
private static void demo_art(string cfgfile, string weightfile, int camIndex) { Network net = Parser.parse_network_cfg(cfgfile); if (string.IsNullOrEmpty(weightfile)) { Parser.load_weights(net, weightfile); } Network.set_batch_network(net, 1); Utils.Rand = new Random(2222222); using (VideoCapture cap = new VideoCapture(camIndex)) { string window = "ArtJudgementBot9000!!!"; if (cap != null) { Utils.Error("Couldn't connect to webcam.\n"); } int i; int[] idx = { 37, 401, 434 }; int n = idx.Length; while (true) { Image ini = LoadArgs.get_image_from_stream(cap); Image inS = LoadArgs.resize_image(ini, net.W, net.H); LoadArgs.show_image(ini, window); float[] p = Network.network_predict(net, inS.Data); Console.Write($"\033[2J"); Console.Write($"\033[1;1H"); float score = 0; for (i = 0; i < n; ++i) { float s = p[idx[i]]; if (s > score) { score = s; } } Console.Write($"I APPRECIATE THIS ARTWORK: %10.7f%%\n", score * 100); Console.Write($"["); int upper = 30; for (i = 0; i < upper; ++i) { Console.Write($"%c", ((i + .5) < score * upper) ? 219 : ' '); } Console.Write($"]\n"); CvInvoke.WaitKey(1); } } }
private static void generate_vid_rnn(string cfgfile, string weightfile) { Network extractor = Parser.parse_network_cfg("cfg/extractor.recon.cfg"); Parser.load_weights(extractor, "/home/pjreddie/trained/yolo-coco.conv"); Network net = Parser.parse_network_cfg(cfgfile); if (string.IsNullOrEmpty(weightfile)) { Parser.load_weights(net, weightfile); } Network.set_batch_network(extractor, 1); Network.set_batch_network(net, 1); int i; VideoCapture cap = new VideoCapture("/extra/vid/ILSVRC2015/Data.Data/VID/snippets/val/ILSVRC2015_val_00007030.mp4"); float[] feat; float[] next; next = null; Image last = null; for (i = 0; i < 25; ++i) { Image im = LoadArgs.get_image_from_stream(cap); Image re = LoadArgs.resize_image(im, extractor.W, extractor.H); feat = Network.network_predict(extractor, re.Data); if (i > 0) { Console.Write($"%f %f\n", Utils.mean_array(feat, 14 * 14 * 512), Utils.variance_array(feat, 14 * 14 * 512)); Console.Write($"%f %f\n", Utils.mean_array(next, 14 * 14 * 512), Utils.variance_array(next, 14 * 14 * 512)); Console.Write($"%f\n", Utils.mse_array(feat, 14 * 14 * 512)); Blas.Axpy_cpu(14 * 14 * 512, -1, feat, next); Console.Write($"%f\n", Utils.mse_array(next, 14 * 14 * 512)); } next = Network.network_predict(net, feat); if (i == 24) { last = new Image(re); } } for (i = 0; i < 30; ++i) { next = Network.network_predict(net, next); Image newi = save_reconstruction(extractor, last, next, "new", i); last = newi; } }
private static void gun_classifier(string datacfg, string cfgfile, string weightfile, int camIndex, string filename) { int[] badCats = { 218, 539, 540, 1213, 1501, 1742, 1911, 2415, 4348, 19223, 368, 369, 370, 1133, 1200, 1306, 2122, 2301, 2537, 2823, 3179, 3596, 3639, 4489, 5107, 5140, 5289, 6240, 6631, 6762, 7048, 7171, 7969, 7984, 7989, 8824, 8927, 9915, 10270, 10448, 13401, 15205, 18358, 18894, 18895, 19249, 19697 }; Console.Write($"Classifier Demo\n"); Network net = Parser.parse_network_cfg(cfgfile); if (string.IsNullOrEmpty(weightfile)) { Parser.load_weights(net, weightfile); } Network.set_batch_network(net, 1); var options = OptionList.read_data_cfg(datacfg); Utils.Rand = new Random(2222222); using (VideoCapture cap = !string.IsNullOrEmpty(filename) ? new VideoCapture(filename) : new VideoCapture(camIndex)) { int top = OptionList.option_find_int(options, "top", 1); string nameList = OptionList.option_find_str(options, "names", ""); string[] names = Data.Data.get_labels(nameList); int[] indexes = new int[top]; if (cap.IsOpened) { Utils.Error("Couldn't connect to webcam.\n"); } float fps = 0; int i; while (true) { var sw = new Stopwatch(); sw.Start(); Image ini = LoadArgs.get_image_from_stream(cap); Image inS = LoadArgs.resize_image(ini, net.W, net.H); LoadArgs.show_image(ini, "Threat Detection"); float[] predictions = Network.network_predict(net, inS.Data); Network.top_predictions(net, top, indexes); Console.Write($"\033[2J"); Console.Write($"\033[1;1H"); bool threat = false; for (i = 0; i < badCats.Length; ++i) { int index = badCats[i]; if (predictions[index] > .01) { Console.Write($"Threat Detected!\n"); threat = true; break; } } if (threat) { Console.Write($"Scanning...\n"); } for (i = 0; i < badCats.Length; ++i) { int index = badCats[i]; if (predictions[index] > .01) { Console.Write($"%s\n", names[index]); } } CvInvoke.WaitKey(10); sw.Stop(); float curr = 1000.0f / sw.ElapsedMilliseconds; fps = .9f * fps + .1f * curr; } } }
private static void threat_classifier(string datacfg, string cfgfile, string weightfile, int camIndex, string filename) { float threat = 0; float roll = .2f; Console.Write($"Classifier Demo\n"); Network net = Parser.parse_network_cfg(cfgfile); if (string.IsNullOrEmpty(weightfile)) { Parser.load_weights(net, weightfile); } Network.set_batch_network(net, 1); var options = OptionList.read_data_cfg(datacfg); Utils.Rand = new Random(2222222); using (VideoCapture cap = !string.IsNullOrEmpty(filename) ? new VideoCapture(filename) : new VideoCapture(camIndex)) { int top = OptionList.option_find_int(options, "top", 1); string nameList = OptionList.option_find_str(options, "names", ""); string[] names = Data.Data.get_labels(nameList); int[] indexes = new int[top]; if (!cap.IsOpened) { Utils.Error("Couldn't connect to webcam.\n"); } float fps = 0; int i; int count = 0; while (true) { ++count; var sw = new Stopwatch(); sw.Start(); Image ini = LoadArgs.get_image_from_stream(cap); if (ini.Data.Length == 0) { break; } Image inS = LoadArgs.resize_image(ini, net.W, net.H); Image outo = ini; int x1 = outo.W / 20; int y1 = outo.H / 20; int x2 = 2 * x1; int y2 = outo.H - outo.H / 20; int border = (int)(.01f * outo.H); int h = y2 - y1 - 2 * border; int w = x2 - x1 - 2 * border; float[] predictions = Network.network_predict(net, inS.Data); float currThreat = 0; currThreat = predictions[0] * 0f + predictions[1] * .6f + predictions[2]; threat = roll * currThreat + (1 - roll) * threat; LoadArgs.draw_box_width(outo, x2 + border, (int)(y1 + .02 * h), (int)(x2 + .5 * w), (int)(y1 + .02 * h + border), border, 0, 0, 0); if (threat > .97) { LoadArgs.draw_box_width(outo, (int)(x2 + .5 * w + border), (int)(y1 + .02 * h - 2 * border), (int)(x2 + .5 * w + 6 * border), (int)(y1 + .02 * h + 3 * border), 3 * border, 1, 0, 0); } LoadArgs.draw_box_width(outo, (int)(x2 + .5 * w + border), (int)(y1 + .02 * h - 2 * border), (int)(x2 + .5 * w + 6 * border), (int)(y1 + .02 * h + 3 * border), (int)(.5 * border), 0, 0, 0); LoadArgs.draw_box_width(outo, x2 + border, (int)(y1 + .42 * h), (int)(x2 + .5 * w), (int)(y1 + .42 * h + border), border, 0, 0, 0); if (threat > .57) { LoadArgs.draw_box_width(outo, (int)(x2 + .5 * w + border), (int)(y1 + .42 * h - 2 * border), (int)(x2 + .5 * w + 6 * border), (int)(y1 + .42 * h + 3 * border), 3 * border, 1, 1, 0); } LoadArgs.draw_box_width(outo, (int)(x2 + .5 * w + border), (int)(y1 + .42 * h - 2 * border), (int)(x2 + .5 * w + 6 * border), (int)(y1 + .42 * h + 3 * border), (int)(.5 * border), 0, 0, 0); LoadArgs.draw_box_width(outo, x1, y1, x2, y2, border, 0, 0, 0); for (i = 0; i < threat * h; ++i) { float ratio = (float)i / h; float r = (ratio < .5f) ? (2 * (ratio)) : 1; float g = (ratio < .5f) ? 1 : 1 - 2 * (ratio - .5f); LoadArgs.draw_box_width(outo, x1 + border, y2 - border - i, x2 - border, y2 - border - i, 1, r, g, 0); } Network.top_predictions(net, top, indexes); string buff = $"/home/pjreddie/tmp/threat_{count:06}"; Console.Write($"\033[2J"); Console.Write($"\033[1;1H"); Console.Write($"\nFPS:%.0f\n", fps); for (i = 0; i < top; ++i) { int index = indexes[i]; Console.Write($"%.1f%%: %s\n", predictions[index] * 100, names[index]); } LoadArgs.show_image(outo, "Threat"); CvInvoke.WaitKey(10); sw.Stop(); float curr = 1000.0f / sw.ElapsedMilliseconds; fps = .9f * fps + .1f * curr; } } }
private static void demo_classifier(string datacfg, string cfgfile, string weightfile, int camIndex, string filename) { Console.Write($"Classifier Demo\n"); Network net = Parser.parse_network_cfg(cfgfile); if (string.IsNullOrEmpty(weightfile)) { Parser.load_weights(net, weightfile); } Network.set_batch_network(net, 1); var options = OptionList.read_data_cfg(datacfg); Utils.Rand = new Random(2222222); using (VideoCapture cap = !string.IsNullOrEmpty(filename) ? new VideoCapture(filename) : new VideoCapture(camIndex)) { int top = OptionList.option_find_int(options, "top", 1); string nameList = OptionList.option_find_str(options, "names", ""); string[] names = Data.Data.get_labels(nameList); int[] indexes = new int[top]; if (cap != null) { Utils.Error("Couldn't connect to webcam.\n"); } float fps = 0; int i; while (true) { var sw = new Stopwatch(); sw.Start(); Image ini = LoadArgs.get_image_from_stream(cap); Image inS = LoadArgs.resize_image(ini, net.W, net.H); LoadArgs.show_image(ini, "Classifier"); float[] predictions = Network.network_predict(net, inS.Data); if (net.Hierarchy != null) { net.Hierarchy.Hierarchy_predictions(predictions, 0, net.Outputs, true); } Network.top_predictions(net, top, indexes); Console.Write($"\033[2J"); Console.Write($"\033[1;1H"); Console.Write($"\nFPS:%.0f\n", fps); for (i = 0; i < top; ++i) { int index = indexes[i]; Console.Write($"%.1f%%: %s\n", predictions[index] * 100, names[index]); } CvInvoke.WaitKey(10); sw.Stop(); float curr = 1000.0f / sw.ElapsedMilliseconds; fps = .9f * fps + .1f * curr; } } }