メインの処理をcで行い、一部の処理をpythonで行うといった方法をしています
cでpythonコードを読み込み
そこで、カメラを使用し物体を見つけた際にtxtファイルを作成し、そこに1と書き込み、
その後、cの方でそのファイルを読み取り、もし値が1だった場合止まるといった処理を行うようにしたいのですが、
fopenでファイルを読み込んだ後、ファイルの中身を用いて1だった場合に条件文を用いて処理をさせたいのですが、どのようにしていいか分からず、つまっています。
ファイルを読み込んで中身の値から条件文を使用している例が見つからず悩んでいます。
c
1(省略) 2int main(void){ 3 4 /* pythonインタプリタを初期化 */ 5 Py_Initialize(); 6 set_path(); 7 8 PyRun_SimpleString(~pythonのコード~ 9 . 10 . 11 「物体を見つけた時」 12 13 "f = open('flag.txt', 'w')\n" 14 "flag = '1'\n" 15 "f.writer(flag)\n" 16 "f.close()\n" 17 . 18 . 19 /* pythonインタプリタを終了 */ 20 Py_Finalize(); 21 return 0; 22} 23 24 25int test(void){ 26 FILE *fp; 27 char buf[256]; 28 29 fp = fopen("test.txt", "r"); 30 31 if(fp!=NULL){ 32 while(fgets(buf, 256, fp) != NULL){ 33 34 「txtファイルの値を用いて条件文を立て、その後処理をしたい」 35 値が1の時 36 stop_flag = 1; //止まる動作 37
コードが見づらくて申し訳ないのですが
txtファイルを経由する以外にpythonで物体を見つけた際、cのほうで条件文を使用しstop_flag = 1にするいい方法があればご教授ください。
c
1#include <stdio.h>//入出力 2#include <math.h> //数学 3#include <unistd.h> // 4#include <stdlib.h> //一般ユーティリティ 5#include <signal.h> //シグナル操作 6#include <pthread.h>// 7#include <getopt.h> // 8#include <time.h> //日付および時間 9#include <Python.h> 10 11 12void set_path(void){ 13 PyObject *sys = PyImport_ImportModule("sys"); 14 PyObject *sys_path = PyObject_GetAttrString(sys, "path"); 15 PyObject *dir = PyUnicode_FromString("."); 16 PyList_Append(sys_path, dir); 17} 18 19int main(void){ 20 21 /* pythonインタプリタを初期化 */ 22 Py_Initialize(); 23 set_path(); 24 25 PyRun_SimpleString("import cv2\n" 26 "import math\n" 27 "import numpy as np\n" 28 "import csv\n" 29 30 "def cv_fourcc(c1, c2, c3, c4):\n" 31 " return (ord(c1) & 255) + ((ord(c2) & 255) << 8) + \ 32 ((ord(c3) & 255) << 16) + ((ord(c4) & 255) << 24)\n" 33 "if __name__ == '__main__':\n" 34 " ESC_KEY = 27\n" 35 " INTERVAL= 33\n" /*# 33*/ 36 " FRAME_RATE = 30\n" /*# fps*/ 37 38 " ORG_WINDOW_NAME = 'ORG'\n" 39 " DST_WINDOW_NAME = 'DST'\n" 40 " FILE_NAME1 = '1.avi'\n" 41 42 " DEVICE_ID = 1\n" 43 44 " cap = cv2.VideoCapture(DEVICE_ID)\n" 45 46 " end_flag, c_frame = cap.read()\n" 47 " height, width, channels = c_frame.shape\n" 48 " image_size = height * width\n" 49 " rec = cv2.VideoWriter(FILE_NAME1, \ 50 cv_fourcc('X', 'V', 'I', 'D'), \ 51 FRAME_RATE, \ 52 (width, height), \ 53 True)\n" 54 55 56 " cv2.namedWindow(ORG_WINDOW_NAME)\n" 57 " cv2.namedWindow(DST_WINDOW_NAME)\n" 58 59 " while end_flag == True:\n" 60 " def contains_lines(rect, lines):\n" /*#2 line 61*/ 62 " cnt = 0\n" 63 " for line in lines:\n" 64 " p1, p2 = tuple(line[:2]), tuple(line[2:])\n" 65 " if cv2.pointPolygonTest(rect, p1, False) >= 0 and \ 66 cv2.pointPolygonTest(rect, p2, False) >= 0:\n" 67 " cnt+=1\n" 68 /*#return True*/ 69 " return cnt>=2\n"//2line over 70 /* " return False\n"*/ 71 72 " def detect(img):\n" 73 " dst = []\n" 74 75 76 " gauss = cv2.GaussianBlur(img, (11, 11), 0)\n" 77 " gray = cv2.cvtColor(gauss, cv2.COLOR_BGR2GRAY)\n" 78 " _, binary = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)\n" 79 80 81 " edges = cv2.Canny(binary, 50, 150)\n" 82 " LSD = cv2.createLineSegmentDetector()\n" 83 " lines, width, prec, nfa = LSD.detect(edges)\n" 84 " if lines is None:\n" 85 " return\n" 86 " lines = np.squeeze(lines, axis=1)\n" 87 88 " _, contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1)\n" 89 " for cnt in contours:\n" 90 " area = cv2.contourArea(cnt)\n" 91 " if not 2500 < area < 10000:\n"//500,1000 92 " continue\n" 93 94 " rotated_rect = cv2.minAreaRect(cnt)\n" 95 " rect = cv2.boxPoints(rotated_rect)\n" 96 " (cx, cy), (width, height), angle = rotated_rect\n" 97 " print('center: ({:.1f}, {:.1f}), size: ({:.1f}, {:.1f}),' 'angle: {:.1f}'.format(cx, cy, width, height, angle))\n" 98 99 " min_edge = min(width, height)\n" 100 " if not 10 < min_edge < 50:\n"//5,25 101 " continue\n" 102 " if contains_lines(rect, lines):\n" 103 " dst.append(rect)\n" 104 105 " return dst\n" 106 " rects = detect(c_frame)\n" 107 " if rects is not None:\n" 108 " for rect in rects:\n" 109 " cv2.drawContours(c_frame, [rect.astype(int)], -1, (0, 255, 0), 2)\n" 110 " print(rect)\n" 111 " print('hakken')\n" 112 113 " f = open('line.txt', 'w')\n" 114 " line_flag = '1'\n" 115 " f.writer(line_flag)\n" 116/* " writer.writerow(line_flag)\n" //1行 117/* " writer.writerow(array2d)\n" //複数行*/ 118 " f.close()\n" 119 120 " cv2.imshow(ORG_WINDOW_NAME, c_frame)\n" 121 " key = cv2.waitKey(INTERVAL)\n" 122 " if key == ESC_KEY:\n" 123 " break\n" 124 125 126 " end_flag, c_frame = cap.read()\n" 127 128 129 " cv2.destroyAllWindows()\n" 130 " cap.release()\n" 131 " rec.release()\n"); 132 133 /* pythonインタプリタを終了 */ 134 Py_Finalize(); 135 return 0; 136} 137
こちらが物体を見つけるためのコードです
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。