· 8 years ago · Mar 06, 2018, 04:58 PM
1â— clocktwist.service - My Pendulum Cavity Twist Logger Service
2Loaded: loaded (/lib/systemd/system/clocktwist.service; enabled)
3 Active: failed (Result: exit-code) since Wed 2017-05-24 01:59:01 UTC; 6h ago
4 Process: 1576 ExecStart=/usr/bin/python /home/pi/trinity_twist.py > /home/pi/twist.log 2>&1 (code=exited, status=1/FAILURE)
5 Main PID: 1576 (code=exited, status=1/FAILURE)
6
7May 24 01:59:01 raspberrypi python[1576]: self.renderer.connect(source)
8May 24 01:59:01 raspberrypi python[1576]: File "/usr/lib/python2.7/dist- packages/picamera/mmalobj.py", line 1467, in connect
9May 24 01:59:01 raspberrypi python[1576]: self._connection = MMALConnection(source, self.inputs[0])
10May 24 01:59:01 raspberrypi python[1576]: File "/usr/lib/python2.7/dist-packages/picamera/mmalobj.py", line 1280, in __init__
11May 24 01:59:01 raspberrypi python[1576]: prefix="Failed to enable connection")
12May 24 01:59:01 raspberrypi python[1576]: File "/usr/lib/python2.7/dist-packages/picamera/exc.py", line 157, in mmal_check
13May 24 01:59:01 raspberrypi python[1576]: raise PiCameraMMALError(status, prefix)
14May 24 01:59:01 raspberrypi python[1576]: picamera.exc.PiCameraMMALError: Failed to enable connection: Out of resources (other than memory)
15
16with picamera.PiCamera() as camera:
17 camera.resolution=(1640,1232)
18 camera.framerate= 30
19 camera.start_recording("{}".format(video_name_h264))
20 camera.wait_recording(record_length)
21 camera.stop_recording()
22
23#Python 3.2.3 (default, Mar 1 2013, 11:53:50)
24 #[GCC 4.6.3] on linux2
25 #Type "copyright", "credits" or "license()" for more information.
26 import os
27 from subprocess import call
28 import datetime
29 import time
30 import traceback
31 import sys
32 import picamera
33 import numpy as np
34 import imageio
35 import matplotlib as mpl
36 mpl.use('Agg')
37 from PIL import Image
38 from matplotlib import pyplot as plt
39 from matplotlib import patches as patch
40 from time import sleep
41 import cv2
42 import smtplib
43 from email.mime.multipart import MIMEMultipart
44 from email.mime.text import MIMEText
45
46 def send_error_email(body,subject):
47 fromaddr = "**********@gmail.com"
48 toaddr = "*********@gmail.com"
49 msg = MIMEMultipart()
50 msg['From'] = fromaddr
51 msg['To'] = toaddr
52 msg['Subject'] = subject
53 msg.attach(MIMEText(body, 'plain'))
54 server = smtplib.SMTP('smtp.gmail.com', 587)
55 server.ehlo
56 server.starttls()
57 server.login(fromaddr, "*******")
58 text = msg.as_string()
59 server.sendmail(fromaddr, toaddr, text)
60 server.quit()
61
62 def clean_image (image):
63 image =cv2.GaussianBlur(image,(9,9),0)
64 thresh = cv2.threshold(image, 220, 255, cv2.THRESH_BINARY)[1]#200 initially
65 #thresh = cv2.erode(thresh, None, iterations=1)
66 #thresh = cv2.dilate(thresh, None, iterations=2)
67 im_clean = thresh
68
69 return im_clean
70 def find_first_blob_coords(contours,imCopy):
71 global blob_found
72 contour_areas = [cv2.contourArea(contour) for contour in contours]
73 max_index = np.argmax(contour_areas)
74 max_contour=contours[max_index]
75
76 contour_moment = cv2.moments(max_contour)
77 if contour_moment['m00']!=0:
78 cx = (contour_moment['m10']/contour_moment['m00'])
79 cy = (contour_moment['m01']/contour_moment['m00'])
80 blob_found=True
81 else:
82 cx=0
83 cy=0
84 blob_found=False
85
86 return cx, cy, blob_found
87
88 def find_blob_coords (frame,index,video_name):
89
90 global cx, cy, blob_found, radius, height, width
91 #Load Image, Resize and Convert To Grayscale
92 im = frame
93 image_name="{}-index{}cleaned.png".format(video_name,index)
94 #im_name="{}-index{}.png".format(video_name,index)
95 #im =cv2.resize(im,None,fx=0.5, fy=0.5, interpolation = cv2.INTER_AREA)
96 height, width, channels = im.shape
97 im =cv2.cvtColor(im, cv2.COLOR_RGB2GRAY )
98 #cv2.imwrite(im_name,im)
99
100 while blob_found==True:
101 #If Blob found in Previous Frame
102 #Set Region of Interest (roi) to look for blob in frame
103 xmin = int(max(0,cx-40))
104 ymin = int(max(0,cy-40))
105 xmax = int(min(width, cx +40))
106 ymax = int(min(height, cy+40))
107
108 roi = im[ymin:ymax,
109 xmin:xmax]
110 #roi=im
111 thresh = clean_image (roi)
112 imCopy = cv2.cvtColor(thresh, cv2.COLOR_GRAY2RGB)
113 contours, hierarchy= cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
114 cv2.CHAIN_APPROX_NONE)
115
116 if len(contours)!=0:
117 cropped_cx, cropped_cy, blob_found= find_first_blob_coords(contours, imCopy)
118 cx = cropped_cx+ xmin
119 cy = cropped_cy+ ymin
120 if blob_found==True:
121 return np.array([index,cx,cy])
122 else:
123 #cv2.imwrite(image_name,roi)#Change im to roi for troubleshooting
124 blob_found=False
125 print "no blob found in cropped frame #{}".format(index)
126 return []
127
128 if blob_found==False:
129 #If Blob not found in Previous Frame
130 #searches whole image rather than ROI
131 thresh = clean_image (im)
132 imCopy = cv2.cvtColor(thresh, cv2.COLOR_GRAY2RGB )
133 contours, hierarchy= cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
134 cv2.CHAIN_APPROX_NONE)
135
136 if len(contours)!=0:
137 cx, cy, blob_found= find_first_blob_coords(contours, imCopy)
138 #cv2.imwrite(image_name,imCopy)
139 if blob_found==True:
140 return np.array([index,cx,cy])
141 else:
142 #cv2.imwrite(image_name,im)
143 blob_found=False
144 print "no blob found in frame #{}".format(index)
145 return []
146 def plot_trajectory(x_coords, y_coords):
147 global index, height, width
148 plt.figure()
149 #plt.axis((0,width,0,height))
150 plt.scatter(x_coords.astype(np.float), -(y_coords.astype(np.float)))
151 numberOfBlobsDetected=x_coords.size
152 plt.title("Laser Trajectory-{}/{}blobs found".format(numberOfBlobsDetected
153 ,index))
154 plt.xlabel("x_coords")
155 plt.ylabel("y_coords")
156 plt.savefig("{}-trajectory.png".format(video_name))
157
158 def trajectory_analysis(results):
159
160 frame_no=results[1:,0]
161 #time= frame_no/5
162 x_coords=results[1:,1]
163 y_coords=results[1:,2]
164 plot_trajectory(x_coords, y_coords)
165 plt.close('all')
166
167 fig, axarr = plt.subplots(2, sharex=True)
168
169 axarr[0].plot(frame_no, x_coords)
170 axarr[0].set_title("Trajectory Coordinates vs Frame Number")
171 axarr[0].set_xlabel("Frame Number")
172 axarr[0].set_ylabel("x coordinate")
173 axarr[1].set_xlabel("Frame Number")
174 axarr[1].set_ylabel("y coordinate")
175 axarr[1].plot(frame_no, y_coords)
176 fig.savefig("{}_analysis.png".format(video_name))
177
178 xmax=max(x_coords.astype(np.float))
179 xmin=min(x_coords.astype(np.float))
180 print "xmax:{}".format(xmax)
181 print "xmin:{}".format(xmin)
182 print "dx :{}".format(xmax-xmin)
183 ## print "ymax:{}".format(max(y_coords))
184 ## print "ymin:{}".format(min(y_coords))
185 ## print "dy :{}".format(max(y_coords)-min(y_coords))
186 index_b=0
187
188 previous_twist=0
189
190 while True:
191 index=0
192 index_found=0
193
194 try:
195 tic= time.time()
196 record_length=5
197 start_time=datetime.datetime.now()
198 start_time_formatted=start_time.strftime("%Y-%m-%d")
199 year_month=start_time.strftime("%Y/%m/")
200 path="/home/pi/Twist/"
201 new_path="{}{}".format(path,year_month)
202 txt_name="{}{}.txt".format(new_path,start_time_formatted)
203
204 #Makes directory if it doesn't exist
205 if not os.path.exists(new_path):
206 os.makedirs(new_path)
207 f=open("{}".format(txt_name), "a+")
208
209 print("n start of analysis")
210 #Initialise Variables to be used by functions
211 blob_found=False
212 cx=cy=radius=index=height=width=0
213 #Names image to be saved with timestamp so it is unique
214 time_now=datetime.datetime.now()
215 time_formatted=time_now.strftime("%Y-%m-%d_%H-%M-%S")
216 ##time_formatted="testing"
217
218 video_name= "/home/pi/camera/{}".format(time_formatted)
219 video_name_h264= "{}.h264".format(video_name)
220 video_name_mp4= "{}.mp4".format(video_name)
221 video_name_txt= "{}.txt".format(video_name)
222
223 #Camera takes picture and saves its name as the time stamp
224 print("taking video")
225 time_recorded= time.time()
226 with picamera.PiCamera() as camera:
227 camera.vflip = True
228 camera.resolution=(1640,1232)
229 camera.framerate= 30
230 camera.start_recording("{}".format(video_name_h264))
231 camera.wait_recording(record_length)
232 camera.stop_recording()
233
234 #convert h264 to mp4 using gpac wrapper so it can be watched on PC/Mac
235 call("MP4Box -fps 30 -add {} {}".format(video_name_h264,video_name_mp4),shell=True)
236
237 #Loads Video
238 vid = cv2.VideoCapture(video_name_mp4)
239 #results=np.array(["index","x-coord","y-coord"])
240
241 #Check if Video was Loaded Properly
242 if not vid.isOpened():
243 print "can't open video"
244 #Iterates through each video frame and finds blob
245 while(vid.isOpened()):
246 index=index+ 1
247 ret, frame= vid.read()
248 frames=int(vid.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT))
249
250 if ret==True:
251 #frame=frame[400:900,500:1100]
252 index_results=find_blob_coords(frame,index,video_name)
253 if index_results!=[]:
254 index_found=index_found+1
255 if index_found==1:
256 results=index_results
257 else:
258 #print index_results
259 results=np.vstack((results, index_results))
260 if index==frames-1:
261 print "last frame"
262 break
263 #print"results {}".format(results)
264 x_coords=results[:,1]
265 xmax=max(x_coords.astype(np.float))
266 xmin=min(x_coords.astype(np.float))
267
268 twist=(xmax-xmin)*0.119
269 time_recorded=time_recorded+record_length/2
270 f.write("{} {} n".format(tic,twist))
271 f.close #saves file
272 f=open("{}".format(txt_name), "a+")
273 os.remove("{}".format(video_name_mp4))
274 print"mp4 deleted"
275
276 if abs(twist-previous_twist)>2:
277 trajectory_analysis(results)
278 else:
279 os.remove("{}".format(video_name_h264))
280 print"h264 deleted"
281 previous_twist=twist
282 time_now=time.time()
283 programtime=time_now-tic
284 print "sleeping"
285 while (int(time.time())-int(tic))<30:
286 sleep(0.001)
287 index_b= index_b+1
288 ## print index_b
289 ## if index_b==2:
290 ## raise picamera.PiCameraMMALError(picamera.mmal.MMAL_ENOSPC)
291
292 except picamera.exc.PiCameraError,err:
293 print "something went wrong"
294 traceback.print_exc(file=sys.stdout)
295 traceback_message=traceback.format_exc()
296 picamera.PiCamera().close()
297 error_message=str(err)
298 body="{}nn{}".format(traceback_message,error_message)
299 subject="Camera Error Alert"
300 send_error_email(body,subject)
301
302 except ValueError,e:
303 print"Value Error"
304 traceback.print_exc(file=sys.stdout)
305 traceback_message=traceback.format_exc()
306 picamera.PiCamera().close()
307 error_message=str(err)
308 body="{}nn{}".format(traceback_message,error_message)
309 subject="Value Error Alert"
310 send_error_email(body,subject)
311
312camera = picamera.PiCamera()
313while true:
314 ...
315 camera.vflip = True
316 camera.resolution = (1640,1232)
317 etc