如何解决使用 chaquopy 在 android studio 中集成 python 代码对象检测代码
我正在尝试使用 chaquopy
在 android studio 中集成 python 代码(对象检测),但在手机上测试时我只得到打印但没有其他任何工作?
这是我运行 logcat
时得到的:
{I/zygote64:Dopartialcodecachecollection,code=117KB,data=91KB/zygote64:Aftercodecache
collection,data=91KBIncreasingcodecachecapacityto512KBI/ViewRootImpl:jank_remveInvalidNodeallthenodeinjanklistisoutoftimeV/AudioManager:playSoundEffecteffectType:0I/python.stdout:creation
of
datasetI/python.stdout:W/InputMethodManager:startInputReason=8V/ActivityThread:
Finishing stop of ActivityRecord{e39245f
token=android.os.BinderProxy@f8d4de
com.example.radar/com.example.radar.MainActivityV/ActivityThread:
Handle window ActivityRecord{e39245f
token=android.os.BinderProxy@f8d4decom.example.radar/com.example.radar.MainActivityvisibility:falseE/BpSurfaceComposerClient:
Failed to transact (-1)E/BpSurfaceComposerClient:Failed to transact
(-1)I/ViewRootImpl: jank_removeInvalidNode all the node in jank list
is out of timeW/InputMethodManager: startInputReason = 1}
这是我运行代码时得到的:
{2021-06-28 12:48:18.640 27425-27505/com.example.radarE/AwareLog:AtomicFileUtilsreadFileLines file not exist: android.util.AtomicFile@4f41434
2021-06-28 12:48:20.389 27425-27475/com.example.radar E/MemoryLeakMonitorManager: MemoryLeakMonitor.jar is not exist!
2021-06-28 12:48:20.391 27425-27425/com.example.radar E/Minikin: Could not get cmap table size!
2021-06-28 12:52:37.473 27425-27425/com.example.radar E/BpSurfaceComposerClient: Failed to transact (-1)
2021-06-28 12:52:37.474 27425-27425/com.example.radar E/BpSurfaceComposerClient: Failed to transact (-1)}
解决方法
import cv2
import numpy as np
import vehicles
import requests
#import time
#from matplotlib import pyplot as plt
#import time
#url="http://192.168.43.1:8080/shot.jpg"
def main():
cnt_up=0
cnt_down=0
print("creation of dataset")
cap=cv2.VideoCapture("C://Users//khaoula//AndroidStudioProjects //RADAR//app//src//main//python//cars.mp4")
print("1111")
#Get width and height of video
w=cap.get(3)
h=cap.get(4)
frameArea=h*w
areaTH=frameArea/400
print("2222")
#Lines
line_up=int(2*(h/5))
line_down=int(3*(h/5))
print("33333")
up_limit=int(1*(h/5))
down_limit=int(4*(h/5))
#print("Red line y:",str(line_down))
#print("Blue line y:",str(line_up))
line_down_color=(255,0)
line_up_color=(255,255)
pt1 = [0,line_down]
pt2 = [w,line_down]
print("44444")
pts_L1 = np.array([pt1,pt2],np.int32)
pts_L1 = pts_L1.reshape((-1,1,2))
pt3 = [0,line_up]
pt4 = [w,line_up]
pts_L2 = np.array([pt3,pt4],np.int32)
pts_L2 = pts_L2.reshape((-1,2))
print("55555")
pt5 = [0,up_limit]
pt6 = [w,up_limit]
pts_L3 = np.array([pt5,pt6],np.int32)
pts_L3 = pts_L3.reshape((-1,2))
pt7 = [0,down_limit]
pt8 = [w,down_limit]
pts_L4 = np.array([pt7,pt8],np.int32)
pts_L4 = pts_L4.reshape((-1,2))
print("666666")
#Background Subtractor
fgbg=cv2.createBackgroundSubtractorMOG2(detectShadows=True)
print("77777")
#Kernals
kernalOp = np.ones((3,3),np.uint8)
kernalOp2 = np.ones((5,5),np.uint8)
kernalCl = np.ones((11,11),np.uint8)
print("88888")
font = cv2.FONT_HERSHEY_SIMPLEX
cars = []
max_p_age = 5
pid = 1
print("99999")
while (cap.isOpened()):
print("kkkkk")
ret,frame=cap.read()
rows1,cols1,_=frame.shape
print(rows1,cols1) # 1280 720
#roi = frame[50:540,200:960]
#cols,rows,_=roi.shape
#print("cols:")
#print(cols) #490
#print("rows:")
#print(rows) #760
for i in cars:
i.age_one()
print("i:")
print(i)
print("10")
fgmask=fgbg.apply(frame)
fgmask2=fgbg.apply(frame)
print("11")
if ret==True:
#Binarization
ret,imBin=cv2.threshold(fgmask,200,255,cv2.THRESH_BINARY)
ret,imBin2=cv2.threshold(fgmask2,cv2.THRESH_BINARY)
#OPening i.e First Erode the dilate
mask=cv2.morphologyEx(imBin,cv2.MORPH_OPEN,kernalOp)
mask2=cv2.morphologyEx(imBin2,cv2.MORPH_CLOSE,kernalOp)
#Closing i.e First Dilate then Erode
mask=cv2.morphologyEx(mask,kernalCl)
mask2 = cv2.morphologyEx(mask2,kernalCl)
print("12")
#Find Contours
countours0,hierarchy=cv2.findContours(mask,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
print("13")
for cnt in countours0:
area=cv2.contourArea(cnt)
if area>areaTH:
####Tracking######
m=cv2.moments(cnt)
cx=int(m['m10']/m['m00'])
cy=int(m['m01']/m['m00'])
x,y,w,h=cv2.boundingRect(cnt)
#cv2.rectangle(roi,(x,y),(x + w,y + h),(0,165,255),5)
crop_frame=frame[y-3:y + h+3,x-3:x + w+3]
new=True
if cy in range(up_limit,down_limit):
for i in cars:
if abs(x - i.getX()) <= w and abs(y - i.getY()) <= h:
new = False
i.updateCoords(cx,cy)
if i.going_UP(line_down,line_up)==True:
cnt_up+=1
print("ID:",i.getId(),'crossed going up ')#at',time.strftime("%c"))
elif i.going_DOWN(line_down,line_up)==True:
cnt_down+=1
print("ID:",'crossed going up')# at',time.strftime("%c"))
break
if i.getState()=='1':
if i.getDir()=='down'and i.getY()>down_limit:
i.setDone()
elif i.getDir()=='up'and i.getY()<up_limit:
i.setDone()
if i.timedOut():
index=cars.index(i)
cars.pop(index)
del i
#file=*****************************************************************************
#file = "C://Users//khaoula//PycharmProjects//Radar//" + "hh.jpg"
#cv2.imwrite(file,i)
if new==True: #If nothing is detected,create new
p=vehicles.Car(pid,cx,cy,max_p_age)
n=str(pid)
cars.append(p)
pid+=1
#cv2.circle(frame,(cx,cy),5,-1)
#while True:
cv2.rectangle(frame,(x+w,y+h),0),2)
#print(img)
#crop_img = roi[y :y + h,x :x + w ]
# s = cv2.imread(str(crop_img))
#n = str(pid) # +"_speed_"+str(sp)
#file = 'C://Users//khaoula//PycharmProjects//Radar//' + n + '.jpg'
#cv2.imwrite(file,roi)
#print("img:")
#print(img)
#file = "C://Users//khaoula//PycharmProjects//Radar//" + "hh.jpg"
#cv2.imwrite(file,img)
for i in cars:
cv2.putText(frame,str(i.getId()),(i.getX(),i.getY()),font,0.3,i.getRGB(),cv2.LINE_AA)
#crop_img = roi[i.getY():i.getY() + h,i.getX():i.getX() + w]
file = 'C://Users//khaoula//PycharmProjects//Radar//pics//' + n + '.jpg'
cv2.imwrite(file,crop_frame)
#cv2.imshow("img",frameArea)
#t = time.strftime("%Y-%m-%d_%H-%M-%S")
#file = "C://Users//khaoula//PycharmProjects//Radar//" + ".jpg"
#cv2.imwrite(file,frameArea)
str_up='UP: '+str(cnt_up)
str_down='DOWN: '+str(cnt_down)
frame=cv2.polylines(frame,[pts_L1],False,line_down_color,thickness=2)
frame=cv2.polylines(frame,[pts_L2],line_up_color,[pts_L3],(255,thickness=1)
frame=cv2.polylines(frame,[pts_L4],thickness=1)
cv2.putText(frame,str_up,(10,40),0.5,2,cv2.LINE_AA)
cv2.putText(frame,str_down,90),cv2.LINE_AA)
cv2.imshow('Frame',frame)
print(str_up)
print(str_down)
if cv2.waitKey(1)&0xff==ord('q'):
break
else:
break
print("mmmmmmmmmm")
cap.release()
return cap
这是我试图集成到 android studio 中的 python 代码,似乎问题来自 while 循环,因为我在 9999 之前获得了所有打印,然后应用程序崩溃了我认为这是因为视频我正在工作,但我不确定
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。