質問編集履歴

1

コードの場所を変えました。エラーメッセージはコピペしました。

2020/02/07 13:41

投稿

ono3
ono3

スコア4

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,356 @@
1
+ ```ここに言語を入力
2
+
3
+ from __future__ import division
4
+
5
+ import time
6
+
7
+
8
+
9
+ import os,sys
10
+
11
+ #sys.path.append(os.getcwd())
12
+
13
+
14
+
15
+
16
+
17
+ #from Adafruit_Python_PCA9685.Adafruit_PCA9685 import Adafruit_PCA9685
18
+
19
+ #from Adafruit_Python_PCA9685 import Adafruit_PCA9685
20
+
21
+ import Adafruit_PCA9685
22
+
23
+
24
+
25
+ pwm = Adafruit_PCA9685.PCA9685()
26
+
27
+ pwm.set_pwm_freq(50)
28
+
29
+
30
+
31
+ import cv2
32
+
33
+ print(cv2)
34
+
35
+
36
+
37
+ import sys
38
+
39
+ import cv2.cv as cv
40
+
41
+ #import cv2
42
+
43
+
44
+
45
+ #import cv2 as cv
46
+
47
+
48
+
49
+ from optparse import OptionParser
50
+
51
+
52
+
53
+
54
+
55
+ def move(degree_1,degree_2):
56
+
57
+ degree_1 = int(degree_1 * 5.27)
58
+
59
+ degree_2 = int(degree_2 * 5.27)
60
+
61
+ pwm.set_pwm(0, 0, degree_1)
62
+
63
+ pwm.set_pwm(1, 0, degree_2)
64
+
65
+
66
+
67
+ # Parameters for haar detection
68
+
69
+ # From the API:
70
+
71
+ # The default parameters (scale_factor=2, min_neighbors=3, flags=0) are tuned
72
+
73
+ # for accurate yet slow object detection. For a faster operation on real video
74
+
75
+ # images the settings are:
76
+
77
+ # scale_factor=1.2, min_neighbors=2, flags=CV_HAAR_DO_CANNY_PRUNING,
78
+
79
+ # min_size=<minimum possible face size
80
+
81
+
82
+
83
+ min_size = (20, 20)
84
+
85
+ image_scale = 2
86
+
87
+ haar_scale = 1.2
88
+
89
+ min_neighbors = 2
90
+
91
+ haar_flags = 0
92
+
93
+
94
+
95
+ def detect_and_draw(img, cascade):
96
+
97
+ # allocate temporary images
98
+
99
+ gray = cv.CreateImage((img.width,img.height), 8, 1)
100
+
101
+ small_img = cv.CreateImage((cv.Round(img.width / image_scale),
102
+
103
+ cv.Round (img.height / image_scale)), 8, 1)
104
+
105
+
106
+
107
+ # convert color input image to grayscale
108
+
109
+ cv.CvtColor(img, gray, cv.CV_BGR2GRAY)
110
+
111
+
112
+
113
+ # scale input image for faster processing
114
+
115
+ cv.Resize(gray, small_img, cv.CV_INTER_LINEAR)
116
+
117
+
118
+
119
+ cv.EqualizeHist(small_img, small_img)
120
+
121
+
122
+
123
+ if(cascade):
124
+
125
+ t = cv.GetTickCount()
126
+
127
+ faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0),
128
+
129
+ haar_scale, min_neighbors, haar_flags, min_size)
130
+
131
+ t = cv.GetTickCount() - t
132
+
133
+ print "detection time = %gms" % (t/(cv.GetTickFrequency()*1000.))
134
+
135
+ if faces:
136
+
137
+ for ((x, y, w, h), n) in faces:
138
+
139
+ # the input to cv.HaarDetectObjects was resized, so scale the
140
+
141
+ # bounding box of each face and convert it to two CvPoints
142
+
143
+ pt1 = (int(x * image_scale), int(y * image_scale))
144
+
145
+ pt2 = (int((x + w) * image_scale), int((y + h) * image_scale))
146
+
147
+ cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0)
148
+
149
+
150
+
151
+ cv.ShowImage("result", img)
152
+
153
+
154
+
155
+ if __name__ == '__main__':
156
+
157
+
158
+
159
+ print('move')
160
+
161
+ move(60,60)
162
+
163
+ time.sleep(1)
164
+
165
+ move(65,60)
166
+
167
+ time.sleep(1)
168
+
169
+ move(65,65)
170
+
171
+ time.sleep(1)
172
+
173
+ move(60,65)
174
+
175
+ time.sleep(1)
176
+
177
+ move(60,55)
178
+
179
+ time.sleep(1)
180
+
181
+ print('stop')
182
+
183
+ print('koko')
184
+
185
+
186
+
187
+
188
+
189
+ parser = OptionParser(usage = "usage: %prog [options] [filename|camera_index]")
190
+
191
+ parser.add_option("-c", "--cascade", action="store", dest="cascade", type="str", help="Haar cascade file, default %default", default = "../data/haarcascades/haarcascade_frontalface_alt.xml")
192
+
193
+ (options, args) = parser.parse_args()
194
+
195
+
196
+
197
+ cascade = cv.Load(options.cascade)
198
+
199
+
200
+
201
+ if len(args) != 1:
202
+
203
+ parser.print_help()
204
+
205
+ sys.exit(1)
206
+
207
+
208
+
209
+ print('koko1')
210
+
211
+
212
+
213
+ input_name = args[0]
214
+
215
+ if input_name.isdigit():
216
+
217
+ capture = cv.CreateCameraCapture(int(input_name))
218
+
219
+ print('koko222')
220
+
221
+ else:
222
+
223
+ capture = None
224
+
225
+ print('koko2')
226
+
227
+
228
+
229
+ print('abspath: ', os.path.abspath(__file__))
230
+
231
+ print('abs dirname: ', os.path.dirname(os.path.abspath(__file__)))
232
+
233
+ print('koko3')
234
+
235
+
236
+
237
+ cv.NamedWindow("result",0)
238
+
239
+ #cv.NamedWindow("aresult")
240
+
241
+
242
+
243
+ print('koko4')
244
+
245
+
246
+
247
+
248
+
249
+ width = 320 #leave None for auto-detection
250
+
251
+ height = 240 #leave None for auto-detection
252
+
253
+
254
+
255
+ if width is None:
256
+
257
+ width = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH))
258
+
259
+ else:
260
+
261
+ cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_WIDTH,width)
262
+
263
+
264
+
265
+ if height is None:
266
+
267
+ height = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT))
268
+
269
+ else:
270
+
271
+ cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_HEIGHT,height)
272
+
273
+
274
+
275
+ if capture:
276
+
277
+ frame_copy = None
278
+
279
+ while True:
280
+
281
+
282
+
283
+ frame = cv.QueryFrame(capture)
284
+
285
+ if not frame:
286
+
287
+ cv.WaitKey(0)
288
+
289
+ break
290
+
291
+ if not frame_copy:
292
+
293
+ frame_copy = cv.CreateImage((frame.width,frame.height),
294
+
295
+ cv.IPL_DEPTH_8U, frame.nChannels)
296
+
297
+
298
+
299
+ # frame_copy = cv.CreateImage((frame.width,frame.height),
300
+
301
+ # cv.IPL_DEPTH_8U, frame.nChannels)
302
+
303
+
304
+
305
+ if frame.origin == cv.IPL_ORIGIN_TL:
306
+
307
+ cv.Copy(frame, frame_copy)
308
+
309
+ else:
310
+
311
+ cv.Flip(frame, frame_copy, 0)
312
+
313
+
314
+
315
+ detect_and_draw(frame_copy, cascade)
316
+
317
+
318
+
319
+ kuri=1
320
+
321
+ print('move')
322
+
323
+ move(60+kuri,60+kuri)
324
+
325
+ time.sleep(1)
326
+
327
+ kuri=-5
328
+
329
+ move(60+kuri,60+kuri)
330
+
331
+ time.sleep(1)
332
+
333
+
334
+
335
+ if cv.WaitKey(10) >= 0:
336
+
337
+ break
338
+
339
+ else:
340
+
341
+ image = cv.LoadImage(input_name, 1)
342
+
343
+ detect_and_draw(image, cascade)
344
+
345
+ cv.WaitKey(0)
346
+
347
+
348
+
349
+ cv.DestroyWindow("result")
350
+
351
+
352
+
1
- ### 前提・実現したいこと
353
+ ```### 前提・実現したいこと
2
354
 
3
355
 
4
356
 
@@ -6,11 +358,17 @@
6
358
 
7
359
  ### 発生している問題・エラーメッセージ
8
360
 
361
+ sudo実行で、
362
+
9
363
  SUDO python m-facedtect.py --cascae=face.xml 0 だと、下記のエラーです。
10
364
 
365
+
366
+
11
367
  Client is not authorized to connect to Server
12
368
 
13
- (result:2824): Gtk-WARNING **: cannot open display: :0.0
369
+ (result:3174): Gtk-WARNING **: cannot open display: :0.0
370
+
371
+
14
372
 
15
373
  調べると、import cv2cv as cv としていて、コードが、
16
374
 
@@ -18,371 +376,43 @@
18
376
 
19
377
  のところです。
20
378
 
21
- sudoなしで、python m-facedtect.py --cascae=face.xml 0 だと、下記のエラーです。
22
-
23
- Traceback (most recent call last)
24
-
25
- File 'm-facedetect.py', line 12,<modle>
26
-
27
- pwm=Adafruit.PCA9685()
28
-
29
- ```
30
-
31
- など、i2cでのサーボモータ関連の部分が、エラーになります。
32
-
33
- ### 該当のソースコード ```python
34
-
35
-
36
-
37
- from __future__ import division
38
-
39
- import time
40
-
41
-
42
-
43
- import os,sys
44
-
45
- #sys.path.append(os.getcwd())
46
-
47
-
48
-
49
-
50
-
51
- #from Adafruit_Python_PCA9685.Adafruit_PCA9685 import Adafruit_PCA9685
52
-
53
- #from Adafruit_Python_PCA9685 import Adafruit_PCA9685
54
-
55
- import Adafruit_PCA9685
56
-
57
-
58
-
59
- pwm = Adafruit_PCA9685.PCA9685()
60
-
61
- pwm.set_pwm_freq(50)
62
-
63
-
64
-
65
- import cv2
66
-
67
- print(cv2)
68
-
69
-
70
-
71
- import sys
72
-
73
- import cv2.cv as cv
74
-
75
- #import cv2
76
-
77
-
78
-
79
- #import cv2 as cv
80
-
81
-
82
-
83
- from optparse import OptionParser
84
-
85
-
86
-
87
-
88
-
89
- def move(degree_1,degree_2):
90
-
91
- degree_1 = int(degree_1 * 5.27)
92
-
93
- degree_2 = int(degree_2 * 5.27)
94
-
95
- pwm.set_pwm(0, 0, degree_1)
96
-
97
- pwm.set_pwm(1, 0, degree_2)
98
-
99
-
100
-
101
- # Parameters for haar detection
102
-
103
- # From the API:
104
-
105
- # The default parameters (scale_factor=2, min_neighbors=3, flags=0) are tuned
106
-
107
- # for accurate yet slow object detection. For a faster operation on real video
108
-
109
- # images the settings are:
110
-
111
- # scale_factor=1.2, min_neighbors=2, flags=CV_HAAR_DO_CANNY_PRUNING,
112
-
113
- # min_size=<minimum possible face size
114
-
115
-
116
-
117
- min_size = (20, 20)
118
-
119
- image_scale = 2
120
-
121
- haar_scale = 1.2
122
-
123
- min_neighbors = 2
124
-
125
- haar_flags = 0
126
-
127
-
128
-
129
- def detect_and_draw(img, cascade):
130
-
131
- # allocate temporary images
132
-
133
- gray = cv.CreateImage((img.width,img.height), 8, 1)
134
-
135
- small_img = cv.CreateImage((cv.Round(img.width / image_scale),
136
-
137
- cv.Round (img.height / image_scale)), 8, 1)
138
-
139
-
140
-
141
- # convert color input image to grayscale
142
-
143
- cv.CvtColor(img, gray, cv.CV_BGR2GRAY)
144
-
145
-
146
-
147
- # scale input image for faster processing
148
-
149
- cv.Resize(gray, small_img, cv.CV_INTER_LINEAR)
150
-
151
-
152
-
153
- cv.EqualizeHist(small_img, small_img)
154
-
155
-
156
-
157
- if(cascade):
158
-
159
- t = cv.GetTickCount()
160
-
161
- faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0),
162
-
163
- haar_scale, min_neighbors, haar_flags, min_size)
164
-
165
- t = cv.GetTickCount() - t
166
-
167
- print "detection time = %gms" % (t/(cv.GetTickFrequency()*1000.))
168
-
169
- if faces:
170
-
171
- for ((x, y, w, h), n) in faces:
172
-
173
- # the input to cv.HaarDetectObjects was resized, so scale the
174
-
175
- # bounding box of each face and convert it to two CvPoints
176
-
177
- pt1 = (int(x * image_scale), int(y * image_scale))
178
-
179
- pt2 = (int((x + w) * image_scale), int((y + h) * image_scale))
180
-
181
- cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0)
182
-
183
-
184
-
185
- cv.ShowImage("result", img)
186
-
187
-
188
-
189
- if __name__ == '__main__':
190
-
191
-
192
-
193
- print('move')
194
-
195
- move(60,60)
196
-
197
- time.sleep(1)
198
-
199
- move(65,60)
200
-
201
- time.sleep(1)
202
-
203
- move(65,65)
204
-
205
- time.sleep(1)
206
-
207
- move(60,65)
208
-
209
- time.sleep(1)
210
-
211
- move(60,55)
212
-
213
- time.sleep(1)
214
-
215
- print('stop')
216
-
217
- print('koko')
218
-
219
-
220
-
221
-
222
-
223
- parser = OptionParser(usage = "usage: %prog [options] [filename|camera_index]")
224
-
225
- parser.add_option("-c", "--cascade", action="store", dest="cascade", type="str", help="Haar cascade file, default %default", default = "../data/haarcascades/haarcascade_frontalface_alt.xml")
226
-
227
- (options, args) = parser.parse_args()
228
-
229
-
230
-
231
- cascade = cv.Load(options.cascade)
232
-
233
-
234
-
235
- if len(args) != 1:
236
-
237
- parser.print_help()
238
-
239
- sys.exit(1)
240
-
241
-
242
-
243
- print('koko1')
244
-
245
-
246
-
247
- input_name = args[0]
248
-
249
- if input_name.isdigit():
250
-
251
- capture = cv.CreateCameraCapture(int(input_name))
252
-
253
- print('koko222')
254
-
255
- else:
256
-
257
- capture = None
258
-
259
- print('koko2')
260
-
261
-
262
-
263
- print('abspath: ', os.path.abspath(__file__))
264
-
265
- print('abs dirname: ', os.path.dirname(os.path.abspath(__file__)))
266
-
267
- print('koko3')
268
-
269
-
270
-
271
- cv.NamedWindow("result",0)
272
-
273
- #cv.NamedWindow("aresult")
274
-
275
-
276
-
277
- print('koko4')
278
-
279
-
280
-
281
-
282
-
283
- width = 320 #leave None for auto-detection
284
-
285
- height = 240 #leave None for auto-detection
286
-
287
-
288
-
289
- if width is None:
290
-
291
- width = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH))
292
-
293
- else:
294
-
295
- cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_WIDTH,width)
296
-
297
-
298
-
299
- if height is None:
300
-
301
- height = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT))
302
-
303
- else:
304
-
305
- cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_HEIGHT,height)
306
-
307
-
308
-
309
- if capture:
310
-
311
- frame_copy = None
312
-
313
- while True:
314
-
315
-
316
-
317
- frame = cv.QueryFrame(capture)
318
-
319
- if not frame:
320
-
321
- cv.WaitKey(0)
322
-
323
- break
324
-
325
- if not frame_copy:
326
-
327
- frame_copy = cv.CreateImage((frame.width,frame.height),
328
-
329
- cv.IPL_DEPTH_8U, frame.nChannels)
330
-
331
-
332
-
333
- # frame_copy = cv.CreateImage((frame.width,frame.height),
334
-
335
- # cv.IPL_DEPTH_8U, frame.nChannels)
336
-
337
-
338
-
339
- if frame.origin == cv.IPL_ORIGIN_TL:
340
-
341
- cv.Copy(frame, frame_copy)
342
-
343
- else:
344
-
345
- cv.Flip(frame, frame_copy, 0)
346
-
347
-
348
-
349
- detect_and_draw(frame_copy, cascade)
350
-
351
-
352
-
353
- kuri=1
354
-
355
- print('move')
356
-
357
- move(60+kuri,60+kuri)
358
-
359
- time.sleep(1)
360
-
361
- kuri=-5
362
-
363
- move(60+kuri,60+kuri)
364
-
365
- time.sleep(1)
366
-
367
-
368
-
369
- if cv.WaitKey(10) >= 0:
370
-
371
- break
372
-
373
- else:
374
-
375
- image = cv.LoadImage(input_name, 1)
376
-
377
- detect_and_draw(image, cascade)
378
-
379
- cv.WaitKey(0)
380
-
381
-
382
-
383
- cv.DestroyWindow("result")
384
-
385
-
379
+
380
+
381
+ sudoなしの実行で、python m-facedtect.py --cascae=face.xml 0 だと、下記のエラーです。
382
+
383
+
384
+
385
+ Traceback (most recent call last):
386
+
387
+ File "m-facedetect.py", line 12, in <module>
388
+
389
+ pwm = Adafruit_PCA9685.PCA9685()
390
+
391
+ File "/usr/local/lib/python2.7/dist-packages/Adafruit_PCA9685-1.0.1-py2.7.egg/Adafruit_PCA9685/PCA9685.py", line 75, in __init__
392
+
393
+ self._device = i2c.get_i2c_device(address, **kwargs)
394
+
395
+ File "/usr/local/lib/python2.7/dist-packages/Adafruit_GPIO-1.0.3-py2.7.egg/Adafruit_GPIO/I2C.py", line 65, in get_i2c_device
396
+
397
+ return Device(address, busnum, i2c_interface, **kwargs)
398
+
399
+ File "/usr/local/lib/python2.7/dist-packages/Adafruit_GPIO-1.0.3-py2.7.egg/Adafruit_GPIO/I2C.py", line 98, in __init__
400
+
401
+ self._bus = Adafruit_PureIO.smbus.SMBus(busnum)
402
+
403
+ File "/usr/local/lib/python2.7/dist-packages/Adafruit_PureIO-0.2.1-py2.7.egg/Adafruit_PureIO/smbus.py", line 97, in __init__
404
+
405
+ self.open(bus)
406
+
407
+ File "/usr/local/lib/python2.7/dist-packages/Adafruit_PureIO-0.2.1-py2.7.egg/Adafruit_PureIO/smbus.py", line 122, in open
408
+
409
+ self._device = open('/dev/i2c-{0}'.format(bus), 'r+b', buffering=0)
410
+
411
+ IOError: [Errno 13] Permission denied: '/dev/i2c-1'
412
+
413
+
414
+
415
+ ### 該当のソースコード 
386
416
 
387
417
  ### 試したこと
388
418