order of the time or event for implement of the code

asked 2019-03-03 00:44:29 -0600

Redhwan gravatar image

updated 2019-03-03 06:55:20 -0600

I need to run the part from code first, then run second part after finish first part

this full code

#!/usr/bin/env python

#This script uses the cv_bridge package to convert images coming on the topic
#sensor_msgs/Image to OpenCV messages and display them on the screen
import math
import rospy
from sensor_msgs.msg import Image
from geometry_msgs.msg import Twist
import cv2, cv_bridge
from go_to_specific_point_on_map1 import move
import time
distance = 0.0
#font = cv2.FONT_HERSHEY_SIMPLEX
face_cascade = cv2.CascadeClassifier('/home/redhwan/1/run-webcam/Face-Detect-Demo-by-Ali-master/haarcascade_frontalface_default.xml' )
#eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

class Face_detection:
    def __init__(self):
        self.bridge = cv_bridge.CvBridge()
#       cv2.namedWindow("window", 1)
#        self.image_sub = rospy.Subscriber('/camera/rgb/image_raw',Image, self.image_callback)
        self.image_sub = rospy.Subscriber('/usb_cam/image_raw',Image, self.image_callback)
        self.cmd_vel_pub = rospy.Publisher('/cmd_vel_mux/input/teleop',Twist, queue_size=1)
        self.twist = Twist()


    def image_callback(self, msg):
        image = self.bridge.imgmsg_to_cv2(msg,desired_encoding='bgr8')
#        frame = cv2.resize(image, (0,0), fx=0.5, fy=0.5, interpolation=cv2.INTER_NEAREST)
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale( gray,scaleFactor=1.1,minNeighbors=5,minSize=(30, 30),flags=cv2.cv2.CASCADE_SCALE_IMAGE)
#        faces = face_cascade.detectMultiScale(gray, 1.3, 5)
        # Add squeres for each 
        for (x, y, w, h) in faces:
            cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
#The proportional controller is implemented in the following four lines which
#is reposible of linear scaling of an error to drive the control output.        
#            err = x - w/2
            self.twist.linear.x = 0.2
#            self.twist.angular.z = -float(err) / 100
            self.cmd_vel_pub.publish(self.twist)


        cv2.imshow('face ', image)

#        cv2.imshow("window", gray)
        cv2.waitKey(3)
#             



        if(type(faces) == tuple):
            time.sleep(2)
            navigator = move(0.5,0) 


rospy.init_node('face_detection')
follower = Face_detection()
rospy.spin()

first part

        for (x, y, w, h) in faces:
            cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
#The proportional controller is implemented in the following four lines which
#is reposible of linear scaling of an error to drive the control output.        
#            err = x - w/2
            self.twist.linear.x = 0.2
#            self.twist.angular.z = -float(err) / 100
            self.cmd_vel_pub.publish(self.twist)

second part:

  if(type(faces) == tuple):
        time.sleep(2)
        navigator = move(0.5,0)

please help me or any suggestion for solving this issue my experience in python not good

thank you in advance

edit retag flag offensive close merge delete