Browse Source

car crash map updates as camera changes

yigit
Yiğit Çolakoğlu 6 years ago
parent
commit
ae34ac151e
2 changed files with 58 additions and 33 deletions
  1. +18
    -9
      MyCity/app/src/main/java/gq/yigit/mycity/CrashMapFragment.java
  2. +40
    -24
      server_side/api/modules/car_crash.py

+ 18
- 9
MyCity/app/src/main/java/gq/yigit/mycity/CrashMapFragment.java View File

@ -27,7 +27,7 @@ import org.json.JSONException;
import org.json.JSONObject;
public class CrashMapFragment extends Fragment implements WebRequest.responseListener {
public class CrashMapFragment extends Fragment implements WebRequest.responseListener, GoogleMap.OnCameraIdleListener {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
@ -88,6 +88,9 @@ public class CrashMapFragment extends Fragment implements WebRequest.responseLis
mMapView.onResume();
activity = this;
FileActions file_manager = new FileActions();
url = file_manager.readFromFile(getContext(),"server.config").trim();
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
@ -106,18 +109,11 @@ public class CrashMapFragment extends Fragment implements WebRequest.responseLis
googleMap.setMyLocationEnabled(true);
}
}
markerPoints = new ArrayList<LatLng>();
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.getUiSettings().setRotateGesturesEnabled(true);
FileActions file_manager = new FileActions();
url = file_manager.readFromFile(getContext(),"server.config").trim();
String url_crashes = url + "/crashes";
WebRequest data_request = new WebRequest(url_crashes,true,new HashMap<String, String>(),0);
data_request.addListener(activity);
data_request.execute();
googleMap.setOnCameraIdleListener(activity);
CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(39.925533,32.866287)).zoom(13).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
@ -127,8 +123,21 @@ public class CrashMapFragment extends Fragment implements WebRequest.responseLis
return rootView;
}
public void onCameraIdle() {
LatLng center = googleMap.getCameraPosition().target;
String url_crashes = url + "/crashes";
HashMap<String,String> params = new HashMap<>();
params.put("lat", String.valueOf(center.latitude));
params.put("lng", String.valueOf(center.longitude));
WebRequest data_request = new WebRequest(url_crashes,false,params,0);
data_request.addListener(this);
data_request.execute();
}
public void receivedResponse(boolean success,String response, int reqid){
if(success){
markerPoints = new ArrayList<LatLng>();
googleMap.clear();
try{
JSONObject crashes = new JSONObject(response);
Iterator<String> iter = crashes.keys();


+ 40
- 24
server_side/api/modules/car_crash.py View File

@ -13,9 +13,8 @@ import os
import io
import itertools
import pickle
import copy
from object_detection.utils import visualization_utils as vis_util
from object_detection.utils import label_map_util
VEHICLE_CLASSES = [3, 6, 8]
@ -149,19 +148,22 @@ def process_img(img_base64):
if right-left > bottom-top:
injured_people += 1
_, buffer = cv2.imencode('.jpg', image_np)
image_process = image_np[:]
vis_util.visualize_boxes_and_labels_on_image_array(
image_process,
output_dict_processed["detection_boxes"],
output_dict_processed["detection_classes"],
output_dict_processed["detection_scores"],
category_index,
min_score_thresh=MIN_SCORE_THRESH,
use_normalized_coordinates=True,
line_thickness=8)
cv2.imshow("a",image_process)
cv2.waitKey(0)
return base64.b64encode(buffer).decode('ascii'), cars_involved, injured_people
# image_process = image_np[:]
# vis_util.visualize_boxes_and_labels_on_image_array(
# image_process,
# output_dict_processed["detection_boxes"],
# output_dict_processed["detection_classes"],
# output_dict_processed["detection_scores"],
# category_index,
# min_score_thresh=MIN_SCORE_THRESH,
# use_normalized_coordinates=True,
# line_thickness=8)
# cv2.imshow("a",image_process)
# cv2.waitKey(0)
for i in range(len(output_dict_processed["detection_classes"])):
output_dict_processed["detection_classes"][i] = category_index[output_dict_processed["detection_classes"][i]]
return base64.b64encode(buffer).decode('ascii'), cars_involved, injured_people,output_dict_processed
class Crash(Resource):
@ -171,7 +173,7 @@ class Crash(Resource):
id = request.form['id']
lat, long = request.form['lat'], request.form['long']
image, car_count, injured = process_img(base64_img)
image, car_count, injured,out = process_img(base64_img)
priority = car_count + injured
if priority > 10:
priority = 10
@ -187,7 +189,8 @@ class Crash(Resource):
'location': {
'latitude': lat,
'longitude': long
}
},
"output_dict":out
}
if id in crashes:
crashes[id].append(crash)
@ -196,16 +199,29 @@ class Crash(Resource):
with open(db_path, 'w') as f:
json.dump(crashes, f, indent=4)
cv2.imshow("a",load_image_into_numpy_array(Image.open(io.BytesIO(base64.b64decode(image)))))
cv2.waitKey(0)
return crash
class Crashes(Resource):
def get(self):
return crashes
def post(self):
process_dict = copy.deepcopy(crashes)
return_dict = {}
for id in process_dict:
for i in range(len(process_dict[id])):
del process_dict[id][i]["img"]
for id in process_dict:
for i in range(len(process_dict[id])):
location = process_dict[id][i]['location']
lat, lng = float(request.form['lat']), float(request.form['lng'])
if abs(float(location['latitude']) - lat) < 0.3 and abs(float(location['longitude']) - lng) < 0.3:
if id in return_dict:
return_dict[id].append(process_dict[id][i])
else:
return_dict[id] = [process_dict[id][i]]
print(return_dict)
return return_dict
class Box:
def __init__(self,coords, type,index):


Loading…
Cancel
Save